package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.TenantContractTemplateDao;
import com.ym.mec.biz.dal.dto.TenantContractTemplateDto;
import com.ym.mec.biz.dal.entity.TenantContractTemplate;
import com.ym.mec.biz.service.TenantContractTemplateService;
import com.ym.mec.biz.service.TenantInfoService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class TenantContractTemplateServiceImpl extends BaseServiceImpl<Integer, TenantContractTemplate> implements TenantContractTemplateService {

    @Autowired
    private TenantContractTemplateDao tenantContractTemplateDao;

    @Autowired
    private TenantInfoService tenantInfoService;

    @Override
    public BaseDAO<Integer, TenantContractTemplate> getDAO() {
        return tenantContractTemplateDao;
    }

    @Override
    public PageInfo<TenantContractTemplateDto> queryPageList(QueryInfo queryInfo) {
        PageInfo<TenantContractTemplateDto> pageInfo = new PageInfo<TenantContractTemplateDto>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<String, Object>();
        MapUtil.populateMap(params, queryInfo);

        List<TenantContractTemplateDto> dataList = null;
        int count = this.findCount(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = tenantContractTemplateDao.queryPageList(params);
        }
        if (count == 0) {
            dataList = new ArrayList<TenantContractTemplateDto>();
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    @Transactional
    public boolean enableContract(Integer id, Integer userId) {
        TenantContractTemplate tenantContractTemplate = tenantContractTemplateDao.get(id);

        if (tenantContractTemplate == null) {
            throw new BizException("未查询到协议模板");
        }

        if (tenantContractTemplate.getStatus()) {
            return true;
        }

        updateVersion(tenantContractTemplate);

        tenantContractTemplateDao.disableContract(tenantContractTemplate.getOwner(), tenantContractTemplate.getType(), tenantContractTemplate.getTenantId());

        tenantContractTemplate.setStatus(true);
        tenantContractTemplate.setUpdateTime(new Date());
        tenantContractTemplate.setModifyBy(userId);

        tenantContractTemplateDao.update(tenantContractTemplate);

        return true;
    }

    @Override
    public TenantContractTemplate queryLatestContractTemplate(Integer tenantId, String owner, String type) {
        return tenantContractTemplateDao.queryLatestContractTemplate(owner, type, tenantId);
    }

    @Override
    @Transactional
    public boolean createContractTemplate(TenantContractTemplate tenantContractTemplate) {
        updateVersion(tenantContractTemplate);
        Date date = new Date();
        tenantContractTemplate.setCreateTime(date);
        tenantContractTemplate.setUpdateTime(date);
        insert(tenantContractTemplate);
        return true;
    }

    @Override
    @Transactional
    public boolean updateContractTempalte(TenantContractTemplate tenantContractTemplate) {
        updateVersion(tenantContractTemplate);
        tenantContractTemplate.setUpdateTime(new Date());
        this.update(tenantContractTemplate);
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void upgradeContractVersion(Integer tenantId) {
        TenantContractTemplate tenantContractTemplate = tenantContractTemplateDao.queryLatestContractTemplate(null, null, tenantId);
        if (Objects.nonNull(tenantContractTemplate)) {
            updateVersion(tenantContractTemplate);
            tenantContractTemplate.setUpdateTime(new Date());
            this.update(tenantContractTemplate);
        }
    }

    private void updateVersion(TenantContractTemplate tenantContractTemplate) {
        Integer tenantId = tenantContractTemplate.getTenantId();
        tenantInfoService.getDao().getLocked(tenantId);
        Integer maxVersion = tenantContractTemplateDao.queryMaxVersion();
        int version = maxVersion == null ? 1 : maxVersion + 1;
        tenantContractTemplate.setVersion(version);
    }
}