package com.sifang.modules.customer.Service;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.dao.CrmCustomerEntryDesignDao;
import com.sifang.modules.customer.dao.CustomerEntryDao;
import com.sifang.modules.customer.entity.CrmCustomerEntryDesign;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.customer.form.AddEntryReqDesignDetail;
import com.sifang.modules.customer.form.CustomerEntryComponentVo;
import com.sifang.modules.sunshine.SystemSetting.dao.ElectricSetDao;
import com.sifang.modules.sunshine.SystemSetting.dao.PlanDao;
import com.sifang.modules.sunshine.SystemSetting.dao.ProjectInstallDao;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmElectricSet;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmPlan;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmProjectInstallWay;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service()
public class CrmCustomerEntryDesignService {

    @Autowired
    private CrmCustomerEntryDesignDao entryDesignDao;
    @Autowired
    ProjectInstallDao installDao;
    @Autowired
    PlanDao planDao;
    @Autowired
    ElectricSetDao electricSetDao;
    @Autowired
    CustomerEntryDao customerDao;

    public void setEntryDesign(List<AddEntryReqDesignDetail> addEntryReqDesignDetails, List<CustomerEntryComponentVo> itemList, CustomerEntry entry) {
            //先删除历史的方案
        entryDesignDao.deleteByEntryId(entry.getId());
            List<CrmCustomerEntryDesign> entryDesigns = new ArrayList<>();
            StringBuilder schemeId = new StringBuilder();
            StringBuilder schemeTypeId = new StringBuilder();
            StringBuilder schemeName = new StringBuilder();
            StringBuilder schemeTypeName = new StringBuilder();

            for (AddEntryReqDesignDetail detail : addEntryReqDesignDetails) {
                CrmCustomerEntryDesign entryDesign = new CrmCustomerEntryDesign();
                entryDesign.setEntryId(entry.getId());
                entryDesign.setProjectId(detail.getProjectId());
                entryDesign.setSchemeId(detail.getSchemeId());
                entryDesign.setSchemeTypeId(detail.getSchemeTypeId());

                schemeId.append(detail.getSchemeId()+",") ;
                schemeTypeId.append(detail.getSchemeTypeId()+",") ;

                if (StringUtil.isNotBlank(detail.getSchemeTypeId())){
                    CrmProjectInstallWay way = installDao.get(detail.getSchemeTypeId());
                    if(way!= null){
                        entryDesign.setSchemeTypeName(way.getName());
                        schemeTypeName.append(way.getName()+",") ;
                    }
                }
                if (StringUtil.isNotBlank(detail.getSchemeId())) {
                    CrmPlan plan = planDao.get(detail.getSchemeId());
                    if (plan != null) {
                        entryDesign.setSchemeName(plan.getName());
                        schemeName.append(plan.getName()+",") ;
                    }
                }
                entryDesign.setDesignDrawings(detail.getDesignDrawings());
                entryDesign.setDesignDrawingsName(detail.getDesignDrawingsName());
                // 设计容量 组件类型是'组件'的 规格 × 数量
                int designCapacity = 0;
                if (itemList!= null) {
                    for (CustomerEntryComponentVo v : itemList) {
                        if ("3".equals(v.getItemType())&&detail.getProjectId().equals(v.getProjectId())) {
                            if(StringUtil.isNotBlank(v.getItemId())){
                                CrmElectricSet crmElectricSet = electricSetDao.get(v.getItemId());
                                if (v.getNum() == null) {
                                    continue;
                                }
                                designCapacity = designCapacity + (v.getNum() * Integer.parseInt(crmElectricSet.getModel()));
                            }
                        }
                    }
                }
                entryDesign.setDesignCapacity(designCapacity);
                detail.setDesignCapacity(designCapacity);

                // 组件类型是'组件'的组件
                List<CustomerEntryComponentVo> zuJianList = new ArrayList<>();
                if (itemList != null) {
                    zuJianList = itemList.stream().filter(v -> "3".equals(v.getItemType())&&detail.getProjectId().equals(v.getProjectId())).collect(Collectors.toList());
                }
                if (!CollectionUtils.isEmpty(zuJianList)) {
                    List<String> itemIds = zuJianList.stream().filter(e -> !e.getItemId().isEmpty()).map(CustomerEntryComponentVo::getItemId).collect(Collectors.toList());
                    List<Record> records = electricSetDao.findByIds(itemIds);
                    List<String> specList = records.stream().map(r -> r.getStr("model")).collect(Collectors.toList());
                    entryDesign.setComponentSpec(StringUtil.join(specList, ","));
                    detail.setComponentSpec(StringUtil.join(specList, ","));
                }

                entryDesigns.add(entryDesign);
            }
            if(schemeId.length()>0){
                schemeId.deleteCharAt(schemeId.length()-1);
            }
            if(schemeTypeId.length()>0){
                schemeTypeId.deleteCharAt(schemeTypeId.length()-1);
            }
            if(schemeName.length()>0){
                schemeName.deleteCharAt(schemeName.length()-1);
            }
            if(schemeTypeName.length()>0){
                schemeTypeName.deleteCharAt(schemeTypeName.length()-1);
            }
            entry.setSchemeId(schemeId.toString());
            entry.setSchemeTypeId(schemeTypeId.toString());
            entry.setSchemeName(schemeName.toString());
            entry.setSchemeTypeName(schemeTypeName.toString());
            customerDao.save(entry);
            entryDesignDao.save(entryDesigns);
    }

}
