package com.sifang.modules.completionrectification.service;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.completionrectification.dao.CompletionDao;
import com.sifang.modules.completionrectification.entity.CrmCompletionInfo;
import com.sifang.modules.completionrectification.vo.CrmCompletionInfoVo;
import com.sifang.modules.customer.Service.CustomerEntryMsgService;
import com.sifang.modules.customer.constant.CustomerEntryConstants;
import com.sifang.modules.customer.constant.SourceConstants;
import com.sifang.modules.customer.dao.CrmCustomerIncomingApplicationDao;
import com.sifang.modules.customer.dao.CustomerEntryDao;
import com.sifang.modules.customer.entity.CrmCustomerIncomingApplication;
import com.sifang.modules.customer.entity.Customer;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.customer.entity.CustomerWarranty;
import com.sifang.modules.order.entity.CrmCustomerWarrantyCard;
import com.sifang.modules.order.entity.CrmOrder;
import com.sifang.modules.partner.service.GuangfaOperatingService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
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.entity.Operator;
import com.sifang.modules.sys.util.LayuiPage;
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.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompletionService extends BaseService {

    @Autowired
    private CompletionDao completionDao;

    @Autowired
    private CrmCustomerIncomingApplicationDao applicationDao;

    @Autowired
    CustomerEntryDao customerDao;

    @Autowired
    private GuangfaOperatingService guangfaOperatingService;

    @Autowired
    private CustomerEntryMsgService msgService;

    public List<Record> getPowerStationByIds(String[] ids){
        return completionDao.getPowerStationByIds(ids);
    }

    @Transactional(rollbackFor=Exception.class)
    public Result saveCompletionData(CrmCompletionInfoVo completionInfoVo){
        Result result=new Result();
        Map<String,Object> map = new HashMap<>();
        User user = UserUtils.getUser();
        String inputPieceId = "";
        String[] idArr = completionInfoVo.getEntryId().split(",");
        CustomerEntry entry = customerDao.get(idArr[0]);
        for(String entryId : idArr){
            CrmCustomerIncomingApplication application = applicationDao.getByEntryId(entryId);
            inputPieceId = application.getInputPieceId()+",";
        }
        inputPieceId = inputPieceId.substring(0,inputPieceId.length()-1);
        map.put("inputPieceId",inputPieceId);//进件编号
        Record province = AddressUtils.getProvinceCode(entry.getProvince());
        map.put("provinceCode",province.getLong("regionalism_code").toString().concat("000000"));//验收区域_省_行政编码
        Record city = AddressUtils.getCityCode(entry.getCity(),province.getLong("ProvinceID"));
        map.put("cityCode",city.getLong("regionalism_code").toString().concat("000000"));//验收区域_市_行政编码
        map.put("areaCode",AddressUtils.getDistrictCode(entry.getArea(),city.getLong("CityID")).toString().concat("000000"));//验收区域_区_行政编码
        map.put("projectOrgCode",entry.getManagementType());//项目公司编码
        map.put("submitterName",entry.getTenantName());//提交人
        map.put("submitterPhone",entry.getMobile());//联系方式
        map.put("zbcl_img",completionInfoVo.getWarrantyCommitmentFile());//质保承诺文件
        map.put("jgyzbg_img",completionInfoVo.getCompletionReport());//竣工验收报告
        map.put("elxnjc_img",completionInfoVo.getElReport());//EL系统性能检测报告
        map.put("xcccnr_img",completionInfoVo.getNoticeReport());//现场抽查整改通知单
        map.put("xccczg_img",completionInfoVo.getReplyReport());//现场抽查整改回复单
        //调用竣工进件接口
        String completionNo = guangfaOperatingService.doPostForGfAcceptInputPiece(map);
        CrmCompletionInfo completionInfo = new CrmCompletionInfo();
        completionInfo.setCompletionNo(completionNo);
        completionInfo.setPowerStationCode(completionInfoVo.getPowerStationCode());
        completionInfo.setPowerStationCount(String.valueOf(idArr.length));
        completionInfo.setProjectCompanyName(entry.getProjectCompanyName());
        completionInfo.setTraderId(entry.getCreateTraderId());
        completionInfo.setCreateTraderName(entry.getCreateTraderName());
        completionInfo.setProvince(entry.getProvince());
        completionInfo.setCity(entry.getCity());
        completionInfo.setArea(entry.getArea());
        completionInfo.setWarrantyCommitmentFile(completionInfoVo.getWarrantyCommitmentFile());
        completionInfo.setCompletionReport(completionInfoVo.getCompletionReport());
        completionInfo.setElReport(completionInfoVo.getElReport());
        completionInfo.setNoticeReport(completionInfoVo.getNoticeReport());
        completionInfo.setReplyReport(completionInfoVo.getReplyReport());
        completionInfo.setEntryId(completionInfoVo.getEntryId());
        completionInfo.setCompletionStatus("10");//10：创建中
        completionInfo.setCreateTime(new Date());
        //保存整改信息
        completionDao.save(completionInfo);
        //修改竣工状态
        for(String entryId : idArr){
            CustomerEntry customerEntry = customerDao.get(entryId);
            customerEntry.setIsCheckAccept("1");
            customerDao.save(customerEntry);
            msgService.saveCustomerEntryMsg(user, customerEntry.getId(), CustomerEntryConstants.ACCEPT_INCOMING, "竣工进件提交");
        }
        result.setCode("200");
        return result;
    }

    public void getCompletionOrderList(LayuiPage<Record> page, Map<String, String> map) {
        List<Record> list = completionDao.getCompletionOrderList(page, map);
        page.setData(list);
        page.setCount(completionDao.countCompletionOrderList(map));
    }

}