package com.sifang.modules.supply.warehouse.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.dao.BondApplyDao;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.warehouse.entity.CrmFittingStockUpApply;
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 CrmFittingStockUpApplyDao extends BaseDao<CrmFittingStockUpApply> {

    @Autowired
    private BondApplyDao bondApplyDao;

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

    public Record stockUpDetail(String id){
        StringBuffer sb=new StringBuffer();
        sb.append(" SELECT a.id,a.number,a.in_warehouse_id,a.total_apply_amount,a.remarks,a.fitting_type, ");
        sb.append(" a.create_by,a.create_name,DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s')AS createTime, ");
        sb.append(" a.current_operator,a.process_instance_id,DATE_FORMAT(a.submit_time,'%Y-%m-%d %H:%i:%s')AS submitTime, ");
        sb.append(" a.status,a.department_id,a.department_name,a.task_name,a.task_id,a.content,a.examine_opinion,a.process_def_id, ");
        sb.append(" a.operate_user_ids,w.name AS inWarehouseName,d.name as `department_name` ");
        sb.append(" FROM `crm_fitting_stock_up_apply` a  ");
        sb.append(" LEFT JOIN `crm_department` d  on d.id=a.department_id ");
        sb.append(" LEFT JOIN `crm_supply_warehouse` w ON a.in_warehouse_id=w.id ");
        sb.append(" WHERE a.id=? ");
        return Db.findFirst(sb.toString(),id);
    }

    public void fittingStockUpList(LayuiPage<Record> page, Map<String, String> map, User user){
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT a.id,a.number,a.in_warehouse_id,a.total_apply_amount,a.status,")
                .line(" DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s')AS createTime,DATE_FORMAT(a.submit_time,'%Y-%m-%d %H:%i:%s')AS submitTime,w.name AS inWarehouseName ")
                .line(" ,a.create_name,a.current_operator,a.process_instance_id,a.department_id,d.name as `department_name`,a.`task_name`,a.`task_id` ")
                .line(" ,d.front_department ,d.name as departmentName,a.create_by ,a.process_def_id ")
                .line(" ,(case when  k.`ASSIGNEE_`='"+user.getId()+"' then 1 else 0 end) as audit ")
                .line(" ,(case when FIND_IN_SET(?,a.operate_user_ids) then 1 else 0 end ) as lookAuth ",user.getId())
                .line(" FROM `crm_fitting_stock_up_apply` a ")
                .line(" LEFT JOIN `crm_supply_warehouse` w ON a.`in_warehouse_id`=w.`id` ")
                .line(" LEFT JOIN `crm_department` d  on d.id=a.department_id")
                .line(" LEFT JOIN `ACT_RU_TASK` k ON a.`process_instance_id`=k.PROC_INST_ID_ AND k.FORM_KEY_='15' and k.ASSIGNEE_ =?",user.getId());
        builder.line(" WHERE a.status<>'5'");
        getConditionMap(map,builder);
        builder.line("order by a.create_time desc");
        if (page != null) {
            builder.line(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
            Long count = fittingStockUpListCount(map,user);
            page.setCount(count);
        }
        List<Record> list= Db.find(builder.build(), builder.params());
        page.setData(list);
    }

    public long fittingStockUpListCount(Map<String, String> map,User user) {
        SqlBuilder sf = new SqlBuilder();
        sf.line(" SELECT COUNT(a.id) FROM `crm_fitting_stock_up_apply` a  ");
        sf.line(" WHERE a.status <>'5' ");
        getConditionMap(map, sf);
        return Db.queryLong(sf.build(), sf.params());
    }

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

        if (StringUtil.isNotBlank(map.get("number"))) {
            sf.like(" AND a.number LIKE ? ", map.get("number"));
        }
        if (StringUtil.isNotBlank(map.get("status"))) {
            sf.line(" AND a.status = ? ", map.get("status"));
        }
        if (StringUtil.isNotBlank(map.get("createTimeMin"))) {
            sf.line("AND a.create_time >= ?", map.get("createTimeMin") + " 00:00:00");
        }
        if (StringUtil.isNotBlank(map.get("createTimeMax"))) {
            sf.line("AND a.create_time <= ?", map.get("createTimeMax") + " 23:59:59");
        }
        if (StringUtil.isNotBlank(map.get("createName"))) {
            sf.like(" AND a.create_name LIKE ? ", map.get("createName"));
        }
    }

}
