package com.sifang.modules.sunshine.SystemSetting.service;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.dao.CrmOrderServiceMeasuresDao;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmOrderServiceMeasures;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.Page;
import com.sifang.modules.sys.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


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

@Service
public class CrmOrderServiceMeasuresService extends BaseService {
    @Autowired
    CrmOrderServiceMeasuresDao serviceMeasuresDao;
    public Page<Record> getServiceMeasuresList(Page<Record> page){
        List<Record> list=serviceMeasuresDao.getServiceMeasuresList(page);
        Long count =serviceMeasuresDao.getCount();
        page.setCount(count);
        page.setList(list);
        return  page;
    }
    public List<Record> getMeasuresList(String service_type){
        return Db.find("select a.service_measures from `crm_order_service_measures` a where a.service_type=?", service_type);
    }

    public String saveMeasure(String jsonData){
        List listData = new Gson().fromJson(jsonData, List.class);
      List<CrmOrderServiceMeasures> list = Lists.newArrayList();
        Map<String, String> map=(Map<String, String>) listData.get(0);
        String service_type=map.get("service_type");
      for (int i = 0; i < listData.size(); i++) {
            Map<String, String> map1 = (Map<String, String>) listData.get(i);
            String service_measures = map1.get("service_measures");
          Long count = getRepeat(service_type,service_measures);
            if (count > 0) {
                return "205";//已存在
            }
            CrmOrderServiceMeasures serviceMeasures =new CrmOrderServiceMeasures();
            serviceMeasures.setServiceType(service_type);
           serviceMeasures.setServiceMeasures(service_measures);
           serviceMeasures.setCreateTime(new Date());
           list.add(serviceMeasures);
        }
      serviceMeasuresDao.save(list);
        return "200";
    }

    public Long getRepeat(String type,String measure){
        Long count=null;
        String sql = "select count(*) from `crm_order_service_measures` a where  a.service_measures='" + measure + "' ";
            count = Db.queryLong(sql);
        return count;
    }

    public String deleMeasure(String service_measures){
        List<Record> list2 = Db.find("select * from crm_order_site_settlement_template where  status='0' and service_step IN ("+ StringUtil.joinInSql(service_measures.split(","))+") ");
        if (list2.size()>0) {
            return "402";
        }
        Db.update("DELETE FROM `crm_order_service_measures` WHERE service_measures IN ("+ StringUtil.joinInSql(service_measures.split(","))+") ");
        return "200";
    }

    public String removeMeasure(String service_measures){
        List<Record> list2 = Db.find("select * from crm_order_site_settlement_template where service_step ='"+service_measures+"' and status='0'");
        if (list2.size()>0) {
            return "402";
        }
        return "200";
    }


    public List<CrmOrderServiceMeasures> getDetailMeasure(String serviceType){
        String sql="select any_value(m.id) as id,any_value(m.service_type) as service_type,m.service_measures,any_value(t.id) as tempId  FROM `crm_order_service_measures` m left join crm_order_site_settlement_template t on  m.service_measures = any_value(t.service_step) and any_value(t.status)='0'  WHERE  any_value(m.service_type)=? GROUP BY m.service_measures ";
        return  Db.query(sql,serviceType);
    }

    public String saveEditMeasure(String jsonData,String service_type){
        List listData = new Gson().fromJson(jsonData, List.class);
        List<String> ids=new ArrayList();
        List<String> measureIds=getIds(service_type);
        String code=null;
       try{
           for(int i=0;i<listData.size();i++){
               Map<String, String> map=(Map<String, String>) listData.get(i);
               if(StringUtil.isNotBlank(map.get("id"))){
                   ids.add(map.get("id"));
                   code=getRepeatMeasure( map.get("id"),map.get("service_measures"));
               }else{
                   Long count = getRepeat(service_type,map.get("service_measures"));
                   if (count > 0) {
                       continue;
                   }
                   CrmOrderServiceMeasures serviceMeasures=new CrmOrderServiceMeasures();
                   serviceMeasures.setServiceType(service_type);
                   serviceMeasures.setServiceMeasures(map.get("service_measures"));
                   serviceMeasures.setCreateTime(new Date());
                   serviceMeasuresDao.save(serviceMeasures);
               }
           }
           if(ids.size()!=measureIds.size()){
               for(int k=0;k<measureIds.size();k++){
                    if(!ids.contains(measureIds.get(k))){
                    String sql="DELETE FROM `crm_order_service_measures` WHERE id=?";
                   Db.update(sql,measureIds.get(k));
                    }
               }
           }

           code="200";
       }catch (Exception e){
           e.printStackTrace();
           code="202";
       }

        return code;
    }
    public String  getRepeatMeasure(String id,String service_measures) {
        CrmOrderServiceMeasures serviceMeasures = serviceMeasuresDao.get(id);
        if (!service_measures.equals(serviceMeasures.getServiceMeasures())) {
            serviceMeasures.setServiceMeasures(service_measures);
            serviceMeasuresDao.save(serviceMeasures);
        }
        return "200";
    }
    public List<String> getIds(String service_type){
        String sql="SELECT id FROM `crm_order_service_measures` WHERE service_type=?";
       return  Db.query(sql,service_type);
    }
}
