package com.sifang.modules.customer.Service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.CustomerEntryConstants;
import com.sifang.modules.customer.dao.ContractManagementDao;
import com.sifang.modules.customer.dao.CustomerEntryDao;
import com.sifang.modules.customer.entity.CrmCustomerContractManagement;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.customer.form.QueryDocumentRes;
import com.sifang.modules.esign.constants.ESignConstants;
import com.sifang.modules.esign.service.ESignSignService;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
public class CustomerContractManagementService extends BaseService {

    @Autowired
    private ContractManagementDao contractManagementDao;

    @Autowired
    private CustomerEntryDao entryDao;

    public void listDate(LayuiPage<Record> page, Map<String, String> map, Operator operator) {
        page.setData(contractManagementDao.selectList(page, map,operator));
        page.setCount(contractManagementDao.countList(map,operator));
    }

    public Record getOneById(Map<String, String> map){
        return contractManagementDao.getOneById(map);
    }

    //删除合同
    @Transactional(rollbackFor = Exception.class)
    public void deleteContract(String ids) {
        String[] idStr = ids.split(",");
        for (String contractId : idStr) {
            CrmCustomerContractManagement contractManagement = contractManagementDao.get(contractId);
            CustomerEntry entry = entryDao.get(contractManagement.getEntryId());
            entry.setSignStatus(CustomerEntryConstants.SIGN_STATUS_UNSTART);
            entryDao.save(entry);
            // 撤销e签宝合同
            if (!ESignConstants.STATUS_COMPLETED.equals(contractManagement.getStatus())) {
                try {
                    ESignSignService.revoke(contractManagement.getSignFlowId());
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("撤销合同失败，需手动处理，合同编号：{}", contractManagement.getContractNumber());
                }
            }
        }
        String userName = CrmUtils.getUserXM(UserUtils.getUser());
        SqlBuilder builder = new SqlBuilder();
        builder.line("insert into crm_customer_contract_management_del");
        builder.line("select *, now(), ? ", userName);
        builder.line("from crm_customer_contract_management ");
        if (idStr.length > 1) {
            builder.inst("where id in ?", idStr);
        }
        else {
            builder.line("where id = ?", idStr[0]);
        }
        Db.update(builder.build(), builder.params());

        Db.update("delete from crm_customer_contract_management where id in (" + StringUtil.joinInSql(idStr)+ ")");
    }


}
