package com.sifang.modules.supply.warehouse.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.warehouse.entity.CrmSupplyAllocationOrder;
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.stereotype.Repository;

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

@Repository
public class CrmSupplyAllocationOrderDao extends BaseDao<CrmSupplyAllocationOrder> {

    public CrmSupplyAllocationOrder getByPurchaseProcessInstanceId(String processInstanceId){
        return getByHql("from CrmSupplyAllocationOrder where processInstanceId =:p1 ", new Parameter(processInstanceId));
    }

    public List<Record> getOrderList(LayuiPage<Record> page, Map<String, String> map, User user){
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.line("  SELECT o.*,d.front_department,DATE_FORMAT(o.create_time,'%Y-%m-%d %H:%i:%s') as createTime ");
        sqlBuilder.line(" ,(case when  k.`ASSIGNEE_`='"+user.getId()+"' then 1 else 0 end) as audit ");
        sqlBuilder.line(" ,(case when FIND_IN_SET(?,o.operate_user_ids) then 1 else 0 end ) as lookAuth ",user.getId());
        sqlBuilder.line(" FROM `crm_supply_allocation_order` o  ");
        sqlBuilder.line(" LEFT JOIN `crm_department` d  on d.id=o.department_id");
        sqlBuilder.line(" LEFT JOIN `act_ru_task` k ON o.`process_instance_id`=k.PROC_INST_ID_ AND k.FORM_KEY_='11' and k.ASSIGNEE_ =? ",user.getId());
        sqlBuilder.line("  WHERE o.status<>'4'  ");
        appendCondition(map,sqlBuilder);
        sqlBuilder.line("order by o.create_time desc");
        if (page != null) {
            sqlBuilder.limit(page.getPage(), page.getLimit());
        }
        return Db.find(sqlBuilder.build(), sqlBuilder.params());
    }

    public Long contOrderList(Map<String, String> map){
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.line("  SELECT count(o.id) ")
                .line(" FROM `crm_supply_allocation_order` o  ")
                .line("  WHERE o.status<>'4'  ");
        appendCondition(map,sqlBuilder);
        return Db.queryLong(sqlBuilder.build(), sqlBuilder.params());
    }

    public void appendCondition(Map<String, String> map,SqlBuilder builder){
        if(StringUtil.checkParamsValid(map.get("ascriptionId"))){
            builder.line("and (o.out_ascription_id =? or o.in_ascription_id =? ) ", map.get("ascriptionId"),map.get("ascriptionId"));
        }
        if(StringUtil.checkParamsValid(map.get("number"))){
             builder.line("and o.code like concat('%', ?, '%')", map.get("number").trim());
           /* builder.line("and o.code =? ", map.get("number").trim());*/
        }
        if(StringUtil.checkParamsValid(map.get("outName"))){
             builder.line("and o.out_name like concat('%', ?, '%')", map.get("outName").trim());
        }
        if(StringUtil.checkParamsValid(map.get("inName"))){
            builder.line("and o.in_name like concat('%', ?, '%')", map.get("inName").trim());
        }
        if(StringUtil.checkParamsValid(map.get("warehouseId"))){
            builder.line("and o.warehouse_id =? ", map.get("warehouseId").trim());
        }
        if(StringUtil.checkParamsValid(map.get("inWarehouseId"))){
            builder.line("and o.in_warehouse_id =? ", map.get("inWarehouseId").trim());
        }
        if(StringUtil.checkParamsValid(map.get("status"))){
            builder.line("and o.order_status =? ", map.get("status").trim());
        }
        if(StringUtil.checkParamsValid(map.get("remarks"))){
            builder.line("and o.remarks like concat('%', ?, '%')", map.get("remarks").trim());
        }

        if (StringUtil.checkParamsValid(map.get("createTimeSta"))) {
            builder.line("and o.create_time >= concat(?, ' 00：00：00')", map.get("createTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("createTimeEnd"))) {
            builder.line("and o.create_time <= concat(?, ' 23：59：59')", map.get("createTimeEnd").trim());
        }

        if (StringUtil.checkParamsValid(map.get("outTimeSta"))) {
            builder.line("and o.out_time >= concat(?, ' 00：00：00')", map.get("outTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("outTimeEnd"))) {
            builder.line("and o.out_time <= concat(?, ' 23：59：59')", map.get("outTimeEnd").trim());
        }

        if (StringUtil.checkParamsValid(map.get("inTimeSta"))) {
            builder.line("and o.in_time >= concat(?, ' 00：00：00')", map.get("inTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("inTimeEnd"))) {
            builder.line("and o.in_time <= concat(?, ' 23：59：59')", map.get("inTimeEnd").trim());
        }

        if (StringUtil.checkParamsValid(map.get("auditTimeSta"))) {
            builder.line("and o.audit_time >= concat(?, ' 00：00：00')", map.get("auditTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("auditTimeEnd"))) {
            builder.line("and o.audit_time <= concat(?, ' 23：59：59')", map.get("auditTimeEnd").trim());
        }


    }
}
