package com.sifang.modules.site.fittingManager.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.sunshine.fittingManager.entity.CrmSiteFittingApply;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.LayuiPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

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

/**
 * @author lzp
 * @date 2019/10/11 14:28
 */
@Repository
public class SiteFittingApplyDao extends BaseDao<CrmSiteFittingApply> {

    /**
     * 备件申请数据
     *
     * @param status 申请状态：0.申请待审核 1.审核通过
     * @return
     */
    public List<Record> getAllApplyList(LayuiPage page, Map<String, String> map, String status, String siteId) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT id, number, site_id, employe_name, employe_id, employe_feedback, auditor, auditor_id, order_id,  ");
        sb.append(" order_number, status, fitting_id, fitting_code, fitting_version, fitting_name, fitting_img, fitting_apply_num, ");
        sb.append(" fitting_audit_num, old_fitting_flag, audit_time, creator,  refuse_reason, audit_marks, return_fitting ");
        sb.append(" ,DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s') as createTime");
        sb.append(" ,DATE_FORMAT(a.audit_time,'%Y-%m-%d %H:%i:%s') as auditTime");
        sb.append(" FROM `crm_site_fitting_apply` a ");
        sb.append(" where a.status=? and a.site_id=?  ");
        sb.append(queryCondition(map));
        sb.append(" order by a.create_time desc ");
        sb.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sb.toString(), status, siteId);
    }

    public long queryApplyDataCount(Map<String, String> map, String status, String siteId) {
        StringBuilder sb = new StringBuilder();
        sb.append("select count(*) from crm_site_fitting_apply a ");
        sb.append(" where a.status=? and a.site_id=?   ");
        sb.append(queryCondition(map));
        return Db.queryLong(sb.toString(), status, siteId);
    }

    public String queryCondition(Map<String, String> map) {
        if (map == null || map.size() == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotBlank(map.get("orderNumber"))) {
            sb.append(" and a.order_number like '%" + map.get("orderNumber").trim() + "%' ");
        }
        if (StringUtils.isNotBlank(map.get("fittingCode"))) {
            sb.append(" and a.fitting_code like '%" + map.get("fittingCode").trim() + "%' ");
        }
        if (StringUtils.isNotBlank(map.get("fittingName"))) {
            sb.append(" and a.fitting_name like '%" + map.get("fittingName").trim() + "%' ");
        }
        if (StringUtils.isNotBlank(map.get("applianceBrand"))) {
            sb.append(" and o.appliance_brand= '" + map.get("applianceBrand") + "'");
        }
        if (StringUtils.isNotBlank(map.get("applianceCategory"))) {
            sb.append(" and o.appliance_category= '" + map.get("applianceCategory") + "'");
        }
        if (StringUtils.isNotBlank(map.get("applianceModel"))) {
            sb.append(" and o.appliance_model like '%" + map.get("applianceModel").trim() + "%' ");
        }

        if (StringUtils.isNotBlank(map.get("employeId"))) {
            sb.append(" and a.employe_id like '%" + map.get("employeId") + "%' ");
        }
        //是否返旧
        if (StringUtils.isNotBlank(map.get("oldFittingFlag"))) {
            sb.append(" and a.old_fitting_flag = '" + map.get("oldFittingFlag") + " ' ");
        }

        /*申请时间*/
        if (StringUtils.isNotBlank(map.get("applyTimeSta"))) {
            sb.append(" and a.create_time >= '" + map.get("applyTimeSta") + " 00:00:00' ");
        }
        if (StringUtils.isNotBlank(map.get("applyTimeEnd"))) {
            sb.append(" and a.create_time <= '" + map.get("applyTimeEnd") + " 23:59:59' ");
        }
        // 发货时间
        if (StringUtils.isNotBlank(map.get("sendTimeSta"))) {
            sb.append("and a.send_time>='" + map.get("sendTimeSta") + " 00:00:00' ");
        }
        if (StringUtils.isNotBlank(map.get("sendTimeEnd"))) {
            sb.append("and a.send_time<='" + map.get("sendTimeEnd") + " 23:59:59' ");
        }

        return sb.toString();
    }

    //服务商获取出入库明细数据
    public List<Record> getSitInAndOutWarehousingList(LayuiPage page, Map<String, String> map) {
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT a.*,f.suit_printer,f.`version`,p.audit_marks ");
        sb.append(" FROM `crm_site_fitting_keep` a");
        sb.append(" LEFT JOIN `crm_site_fitting` f ON f.id=a.`fitting_id`");
        sb.append("  LEFT JOIN `crm_site_fitting_apply` p ON  p.id=a.relate_record_id");
        sb.append(" WHERE a.site_id=? ");
        sb.append(queryCondition2(map));
        sb.append(" ORDER BY a.create_time DESC");
        sb.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sb.toString(), map.get("siteId"));
    }

    public long getSitInAndOutWarehousingCount(Map<String, String> map) {
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT COUNT(a.id) ");
        sb.append(" FROM `crm_site_fitting_keep` a");
        sb.append(" LEFT JOIN `crm_site_fitting` f ON f.id=a.`fitting_id`");
        sb.append("  LEFT JOIN `crm_site_fitting_apply` p ON  p.id=a.relate_record_id");
        sb.append(" WHERE a.site_id=? ");
        sb.append(queryCondition2(map));
        return Db.queryLong(sb.toString(), map.get("siteId"));
    }

    public String queryCondition2(Map<String, String> map) {

        if (map == null || map.size() == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        if (StringUtil.isNotBlank(map.get("type"))) {  //明细类型
            sb.append(" and a.type='" + map.get("type") + "'");
        }
        if (StringUtils.isNotBlank(map.get("fittingCode"))) {   //物料号
            sb.append(" and a.fitting_code like '%" + map.get("fittingCode").trim() + "%'");
        }
        if (StringUtils.isNotBlank(map.get("fittingName"))) { //备件名称
            sb.append(" and a.fitting_name like '%" + map.get("fittingName").trim() + "%'");
        }
        if (StringUtils.isNotBlank(map.get("fittingVersion"))) {  // 备件型号
            sb.append(" and f.version like '%" + map.get("fittingVersion").trim() + "%'");
        }
        if (StringUtils.isNotBlank(map.get("orderNumber"))) {  // 工单编号
            sb.append(" and p.order_number like '%" + map.get("orderNumber").trim() + "%'");
        }
        if (StringUtils.isNotBlank(map.get("suitPrinter"))) {  //适用机型
            sb.append(" and f.suit_printer like '%" + map.get("suitPrinter").trim() + "%'");
        }
        if (StringUtils.isNotBlank(map.get("applicant"))) {  //申请人
            sb.append(" and a.applicant='" + map.get("applicant") + "'");
        }
        if (StringUtils.isNotBlank(map.get("remarks"))) {  // 备注
            sb.append(" and p.audit_marks like '%" + map.get("remarks").trim() + "%'");
        }

        if (StringUtils.isNotBlank(map.get("operateTimeStart"))) {  //出入库时间
            sb.append(" and a.operate_time >= '" + map.get("operateTimeStart") + " 00:00:00'");
        }
        if (StringUtils.isNotBlank(map.get("operateTimeEnd"))) {
            sb.append(" and a.operate_time <= '" + map.get("operateTimeEnd") + " 23:59:59'");
        }

        return sb.toString();
    }

}
