package com.sifang.modules.sunshine.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.UserUtils;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import org.springframework.stereotype.Repository;

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

@Repository
public class CrmSiteFittingApplyDao extends BaseDao<CrmSiteFittingApply> {

    /**
     * 备件申请数据
     * @param status 申请状态：1.待审核 2.待出库
     * @return
     */
    public List<Record> getAllApplyList(LayuiPage<Record> page, Map<String, String> map, String status) {
        String siteId = CrmUtils.getSiteId(UserUtils.getUser());
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT a.* from crm_platform_fitting_apply a where  a.site_id='"+siteId+"'");
        if (StringUtil.isNotBlank(status)) {
            sb.append(" and a.status = '" + status + "' ");
        } else {
            sb.append(" and a.status != 6 ");
        }
        sb.append(search(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());
    }

    public long queryApplyDataCount(Map<String, String> map, String status) {
        String siteId = CrmUtils.getSiteId(UserUtils.getUser());
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT count(*) from crm_platform_fitting_apply a where  a.site_id='"+siteId+"' ");
        if (StringUtil.isNotBlank(status)) {
            sb.append(" and a.status = '" + status + "' ");
        } else {
            sb.append(" and a.status != 6 ");
        }
        sb.append(search(map));
        return Db.queryLong(sb.toString());
    }

    public String search(Map<String, String> map) {
        StringBuilder sb = new StringBuilder("");
        if (map != null) {
            if (StringUtil.checkParamsValid(map.get("fittingCode"))) {
                sb.append(" and a.fitting_code like '%" + map.get("fittingCode").trim() + "%' ");
            }
            if (StringUtil.checkParamsValid(map.get("fittingName"))) {
                sb.append(" and a.fitting_name like '%" + map.get("fittingName").trim() + "%' ");
            }
            if (StringUtil.checkParamsValid(map.get("fittingVersion"))) {
                sb.append(" and a.fitting_version like '%" + map.get("fittingVersion").trim() + "%' ");
            }
            if (StringUtil.checkParamsValid(map.get("suitPrinter"))) {
                sb.append(" and a.suit_printer like '%" + map.get("suitPrinter").trim() + "%' ");
            }
            if(StringUtil.checkParamsValid(map.get("applyMarks"))){
                sb.append(" and a.apply_marks like '%" + map.get("applyMarks")+ "%'");
            }
            if(StringUtil.checkParamsValid(map.get("factoryMarks"))){
                sb.append(" and a.factory_marks like '%" + map.get("factoryMarks")+ "%'");
            }
            if(StringUtil.checkParamsValid(map.get("status"))){
                sb.append(" and a.status = '" + map.get("status")+ "'");
            }
            /*申请时间*/
            if(StringUtil.checkParamsValid( map.get("applyTimeSta"))){
                sb.append(" and a.create_time >= '" + map.get("applyTimeSta") + " 00:00:00' ");
            }
            if(StringUtil.checkParamsValid( map.get("applyTimeEnd"))){
                sb.append(" and a.create_time <= '" + map.get("applyTimeEnd") + " 23:59:59' ");
            }
            if(StringUtil.checkParamsValid( map.get("confirmTimeMin"))){
                sb.append(" and a.confirm_time >= '" + map.get("confirmTimeMin") + " 00:00:00' ");
            }
            if(StringUtil.checkParamsValid( map.get("confirmTimeMax"))){
                sb.append(" and a.confirm_time <= '" + map.get("confirmTimeMax") + " 23:59:59' ");
            }
            if(StringUtil.checkParamsValid( map.get("outTimeMin"))){
                sb.append(" and a.out_fitting_time >= '" + map.get("outTimeMin") + " 00:00:00' ");
            }
            if(StringUtil.checkParamsValid( map.get("outTimeMax"))){
                sb.append(" and a.out_fitting_time <= '" + map.get("outTimeMax") + " 23:59:59' ");
            }
            if(StringUtil.checkParamsValid( map.get("sendTimeMin"))){
                sb.append(" and a.send_time >= '" + map.get("sendTimeMin") + " 00:00:00' ");
            }
            if(StringUtil.checkParamsValid( map.get("sendTimeMax"))){
                sb.append(" and a.send_time <= '" + map.get("sendTimeMax") + " 23:59:59' ");
            }
        }
        return sb.toString();
    }

    public List<Record> getOrderApplyList(String orderId) {
        StringBuilder sb = new StringBuilder();
        sb.append("select a.*,r.status AS refundStatus from crm_site_fitting_apply a  LEFT JOIN `crm_platform_fitting_refund` r ON r.apply_id=a.id AND r.status!=0  where a.order_id = '" + orderId + "' AND a.status IN('0','1') ");
        return Db.find(sb.toString());
    }

}
