package com.sifang.modules.flowpath.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.flowable.utils.FormUtils;
import com.sifang.modules.flowpath.entity.BondApply;
import com.sifang.modules.flowpath.entity.PriceCoefficientApply;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

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

@Repository
public class PriceCoefficientApplyDao extends BaseDao<PriceCoefficientApply> {

    @Autowired
    private BondApplyDao bondApplyDao;

    public PriceCoefficientApply getByApplyId(String applyId){
        return getByHql("from PriceCoefficientApply where id =:p1 ", new Parameter(applyId));
    }

    public Record getDetailById(String id){
        StringBuffer sb=new StringBuffer();
        sb.append(" SELECT a.id,a.remarks ,a.task_id,a.coefficient,a.apply_number ");
        sb.append(" ,a.process_instance_id,a.status,a.examine_opinion,a.content,DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s')AS createTime,a.create_name,d.name as department_name ");
        sb.append(" ,t.name as materialTypeName,a.material_type_id FROM `crm_price_coefficient_apply` a ");
        sb.append(" LEFT JOIN `crm_department` d  on d.id=a.department_id");
        sb.append(" left join crm_supply_material_type t on t.id=a.material_type_id ");
        sb.append(" where a.id = ?");
        return Db.findFirst(sb.toString(),id);
    }


    public void applyList(LayuiPage<Record> page, Map<String, String> map, User user){
        SqlBuilder sf = new SqlBuilder();
        sf.line(" SELECT a.id,a.remarks, a.create_time,a.create_name,a.task_name,a.task_id ,a.status,a.create_by,a.department_id");
        sf.line(" ,DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s')AS createTime,a.coefficient,a.apply_number  ");
        sf.line(" ,a.process_def_id,a.department_name as departmentName ,DATE_FORMAT(a.finish_time,'%Y-%m-%d %H:%i:%s')AS finishTime,t.name as materialTypeName ");
        sf.line(" ,(case when  k.`ASSIGNEE_`='"+user.getId()+"' then 1 else 0 end) as audit ");
        sf.line(" ,(case when FIND_IN_SET(?,a.operate_user_ids) then 1 else 0 end ) as lookAuth ",user.getId());
        sf.line(" FROM `crm_price_coefficient_apply` a ");
        sf.line(" LEFT JOIN `act_ru_task` k ON a.`process_instance_id`=k.PROC_INST_ID_ AND k.FORM_KEY_='14' and k.ASSIGNEE_ =? ",user.getId());
        sf.line(" left join crm_supply_material_type t on t.id=a.material_type_id ");
        sf.line(" WHERE a.status <>'4' ");
        getConditionMap(map, sf);
        sf.line(" ORDER BY a.status='3',a.create_time DESC  ");
        if (page != null) {
            sf.line(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
            Long count = getCount(map,user);
            page.setCount(count);
        }
        List<Record> list = Db.find(sf.build(), sf.params());
        page.setData(list);
    }

    public long getCount(Map<String, String> map,User user) {
        SqlBuilder sf = new SqlBuilder();
        sf.line(" SELECT COUNT(a.id) FROM `crm_price_coefficient_apply` a ");
        sf.line(" LEFT JOIN `act_ru_task` k ON a.`process_instance_id`=k.PROC_INST_ID_ AND k.FORM_KEY_='14'  ");
        sf.line("  WHERE a.status<>'4' ");
        getConditionMap(map, sf);
        return Db.queryLong(sf.build(), sf.params());
    }

    public void getConditionMap(Map<String, String> map, SqlBuilder sf) {

        if (StringUtil.isNotBlank(map.get("applyNumber"))) {
            sf.like(" AND a.apply_number LIKE ? ", map.get("applyNumber"));
        }
        if (StringUtil.isNotBlank(map.get("remarks"))) {
            sf.like(" AND a.remarks LIKE ? ", map.get("remarks"));
        }
        if (StringUtil.isNotBlank(map.get("createTimeSta"))) {
            sf.line("AND a.create_time >= ?", map.get("createTimeSta") + " 00:00:00");
        }
        if (StringUtil.isNotBlank(map.get("createTimeEnd"))) {
            sf.line("AND a.create_time <= ?", map.get("createTimeEnd") + " 23:59:59");
        }
    }

    public long getNormalCount(String materialTypeId) {
        return Db.queryLong(" SELECT COUNT(a.id) FROM `crm_price_coefficient_apply` a WHERE a.status in('1','2','3') and a.material_type_id=?", materialTypeId);
    }

    public List<Record> getEffectApply() {
        return Db.find(" SELECT a.id,a.material_type_id,a.coefficient  FROM crm_price_coefficient_apply a WHERE a.status='3' ");
    }
}
