package com.sifang.modules.contract.service;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.contract.dao.TraderContractTemplateDao;
import com.sifang.modules.contract.entity.TraderContractTemplate;
import com.sifang.modules.customer.constant.CustomerEntryConstants;
import com.sifang.modules.customer.constant.PartnerConstants;
import com.sifang.modules.esign.cn.tsign.hz.exception.EsignDemoException;
import com.sifang.modules.esign.service.ESignFileService;
import com.sifang.modules.esign.vo.FileVo;
import com.sifang.modules.partner.utils.PartnerApiUtils;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.util.LayuiPage;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ContractTemplateService extends BaseService {

    @Autowired
    private TraderContractTemplateDao traderContractTemplateDao;

   public void contractTemplateData(LayuiPage<Record> page, Map<String, String> map){
       List<Record> list=traderContractTemplateDao.contractTemplateList(page, map);
       long count=traderContractTemplateDao.getContractTemplateCount(map);
       page.setData(list);
       page.setCount(count);
   }

   public FileVo saveContractTemplate(String fileId,String name,String type,String companyId,String patern,String filingType) throws EsignDemoException {
       logger.info(">>>>>>>>>>>>saveContractTemplate  fileId:{},name:{},type:{}",fileId,name,type);
       FileVo fileVo= ESignFileService.getTemplateCreateUrl(fileId,name);
       TraderContractTemplate contractTemplate=new TraderContractTemplate();
       contractTemplate.setType(type);
       contractTemplate.setCompanyId(companyId);
       contractTemplate.setContractName(name);
       contractTemplate.setPatern(patern);
       // 业主签名识别文字
       if("3".equals(type)){
           switch (patern) {
               case PartnerConstants.PARTNER_ID_YX:
                   contractTemplate.setSignKey("授权人（签名）：");
                   break;
               case PartnerConstants.PARTNER_ID_HR:
                   contractTemplate.setSignKey("授权人为个人时(签名/电子签章)：");
                   break;
               case PartnerConstants.PARTNER_ID_GF:
                   contractTemplate.setSignKey("本人（签字）：");
                   break;
           }
       }else if("2".equals(type)){
           if(CustomerEntryConstants.FILING_TYPE_FARMER.equals(filingType)){
               switch (patern) {
                   case PartnerConstants.PARTNER_ID_HR:
                       contractTemplate.setSignKey("甲方（签字捺印）:");
                       break;
               }
           }else if(CustomerEntryConstants.FILING_TYPE_COMPANY.equals(filingType)){
               switch (patern) {
                   case PartnerConstants.PARTNER_ID_HR:
                       contractTemplate.setSignKey("甲方（电子签名/签字捺印）:");
                       break;
               }
           }
       }

       contractTemplate.setDocTemplateId(fileVo.getFileId());
       contractTemplate.setCreateTime(new Date());
       contractTemplate.setStatus("0");
       contractTemplate.setFilingType(filingType);
       User user= UserUtils.getUser();
       contractTemplate.setCreateBy(user.getId());
       traderContractTemplateDao.save(contractTemplate);
       return fileVo;
   }

   public boolean checkTemplate(String companyId,String type, String patern,String filingType){
       long count=traderContractTemplateDao.checkTemplate(companyId,type,patern,filingType);
       return count>0;
   }

   public void deleteContractTemplate(String id){
       logger.info(">>>>>>>>>>>>>>>>>deleteContractTemplate  id:{}",id);
       traderContractTemplateDao.deleteContractTemplate(id);
   }

   public int positionSet(String id,Integer positionPage,Integer positionAcross,Integer positionVertical){
       SQLQuery sqlQuery = traderContractTemplateDao.getSession().createSQLQuery("UPDATE crm_trader_contract_template SET position_page =:positionPage, position_across =:positionAcross,position_vertical=:positionVertical  WHERE  id=:id ");
       sqlQuery.setParameter("positionPage", positionPage);
       sqlQuery.setParameter("positionAcross",positionAcross);
       sqlQuery.setParameter("positionVertical",positionVertical);
       sqlQuery.setParameter("id", id);
       int i=sqlQuery.executeUpdate();
       return i;
   }
    public int positionSet2(String id,Integer positionPage,Integer positionAcross,Integer positionVertical,Integer positionPage2,Integer positionAcross2,Integer positionVertical2){
        SQLQuery sqlQuery = traderContractTemplateDao
                .getSession()
                .createSQLQuery("UPDATE crm_trader_contract_template SET position_page =:positionPage, " +
                        "position_across =:positionAcross,position_vertical=:positionVertical, " +
                        " position_page2 = :positionPage2,position_across2 =:positionAcross2,position_vertical2=:positionVertical2 "+
                        " WHERE  id=:id ");
        sqlQuery.setParameter("positionPage", positionPage);
        sqlQuery.setParameter("positionAcross",positionAcross);
        sqlQuery.setParameter("positionVertical",positionVertical);
        sqlQuery.setParameter("positionPage2", positionPage2);
        sqlQuery.setParameter("positionAcross2",positionAcross2);
        sqlQuery.setParameter("positionVertical2",positionVertical2);
        sqlQuery.setParameter("id", id);
        int i=sqlQuery.executeUpdate();
        return i;
    }
}
