package com.sifang.modules.customer.Service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.dao.ComposePlanDao;
import com.sifang.modules.customer.entity.ComposePlan;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.customer.form.AddEntryReq;
import com.sifang.modules.sunshine.SystemSetting.dao.PlanDao;
import com.sifang.modules.sunshine.SystemSetting.dao.ProjectInstallDao;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.common.service.BaseService;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ComposePlanService extends BaseService {

    @Autowired
    private ComposePlanDao composePlanDao;

    @Autowired
    private ProjectInstallDao projectInstallDao;

    @Autowired
    private PlanDao planDao;


    /**
     * 清除当前项目下的方案信息
    */
    public void deleteComposePlanByEntryId(String entryId){
        SQLQuery sqlQuery = composePlanDao.getSession().createSQLQuery("DELETE FROM crm_compose_plan WHERE customer_entry_id=:entryId");
        sqlQuery.setParameter("entryId", entryId);
        sqlQuery.executeUpdate();
    }

    /**
     * 保存项目方案信息
     * 返回项目方案名称,多个'/'隔开
     */
    public void saveComposePlan(CustomerEntry entry, String entryId){
        List<ComposePlan> lists = entry.getComposePlanLists();
//        String planName = "";
        if (lists.size() > 0) {
            for (ComposePlan plan : lists) {
//                if(plan.getComponentNum() ==null){
//                    plan.setComponentNum(0);
//                }
                if(StringUtil.isNotBlank(plan.getPlanTypeId())){
                    plan.setPlanType(projectInstallDao.get(plan.getPlanTypeId()).getName());
                }
                if(StringUtil.isNotBlank(plan.getPlanId())){
                    plan.setName(planDao.get(plan.getPlanId()).getName());
                }
                plan.setCustomerEntryId(entryId);
            }
            composePlanDao.save(lists);
            List<String> planNameList = lists.stream().map(ComposePlan::getName).collect(Collectors.toList());
            List<String> planId = lists.stream().map(ComposePlan::getPlanId).collect(Collectors.toList());
            entry.setSchemeName(StringUtil.join(planNameList, ","));
            entry.setSchemeId(StringUtil.join(planId, ","));
        }
        // return planName;
    }

    public List<ComposePlan> findByEntryId(String entryId){
        DetachedCriteria beautyCriteria = DetachedCriteria.forClass(ComposePlan.class, "b");
        beautyCriteria.add(Restrictions.eq("b.customerEntryId",entryId));
        return composePlanDao.find(beautyCriteria);
    }

    public Record findByEntrySchemeId(String id){
        return Db.findFirst("SELECT *,id as plan_id  FROM `crm_plan`WHERE id=? ",id);
    }

    public List<Record> findPlanInfoEntryId(String entryId) {
        return composePlanDao.findPlanInfoEntryId(entryId);
    }
}
