package com.sifang.modules.site.fittingManager.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.fittingManager.entity.CrmPlatformFittingRefund;
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 SiteFittingRefundDao extends BaseDao<CrmPlatformFittingRefund> {

    public List<Record> getSIteFittingRefundList(LayuiPage<Record> page, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT 	id, number, site_id,  site_name, marks, apply_id, order_id, order_number, fitting_type, status, ");
        sf.append("  fitting_code, fitting_version, fitting_name, fitting_img, refund_num,   logistics_names, ");
        sf.append("   logistics_number, logistics_imgs,   creator,  postage_type, delivery_name, delivery_mobile, delivery_address ");
        sf.append(" ,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') as create_time");
        sf.append(" ,DATE_FORMAT(end_time,'%Y-%m-%d %H:%i:%s') as end_time");
        sf.append(" ,DATE_FORMAT(send_time,'%Y-%m-%d %H:%i:%s') as send_time");
        sf.append("  FROM crm_platform_fitting_refund ");
        sf.append(" WHERE status IN ('1','2') and  site_id =? ");
        sf.append(queryCondition(map));
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString(), map.get("siteId"));
    }

    public long getSIteFittingRefundCount(Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT 	count(id) ");
        sf.append("  FROM crm_platform_fitting_refund ");
        sf.append(" WHERE status IN ('1','2') and  site_id =? ");
        sf.append(queryCondition(map));
        return Db.queryLong(sf.toString(), map.get("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 order_number like '%" + map.get("orderNumber").trim() + "%'");
        }
        if (StringUtils.isNotBlank(map.get("fittingCode"))) {
            sb.append(" and fitting_code like '%" + map.get("fittingCode").trim() + "%'");
        }
        if (StringUtils.isNotBlank(map.get("fittingName"))) {
            sb.append(" and fitting_name like '%" + map.get("fittingName").trim() + "%'");
        }
        //返厂类型
        if (StringUtils.isNotBlank(map.get("fittingType"))) {
            sb.append(" and fitting_type = '" + map.get("fittingType") + "' ");
        }
        //到厂状态
        if (StringUtils.isNotBlank(map.get("status"))) {
            sb.append("and status= '" + map.get("status") + "'");
        }
        /*申请时间*/
        if (StringUtils.isNotBlank(map.get("applyTimeSta"))) {
            sb.append(" and create_time >= '" + map.get("applyTimeSta") + " 00:00:00' ");
        }
        if (StringUtils.isNotBlank(map.get("applyTimeEnd"))) {
            sb.append(" and create_time <= '" + map.get("applyTimeEnd") + " 23:59:59' ");
        }
        return sb.toString();
    }

    //获取服务商端的旧件信息
    public List<Record> getOldFittingList(LayuiPage page, Map<String, String> map) {
        StringBuffer sb = new StringBuffer();
        sb.append("select a.* ");
        sb.append(",DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s') as createTime ");
        sb.append(" ,d.number as orderNumber,d.power_station_name,d.warranty_type ,d.employe_name,d.customer_name,d.province,d.city,d.area,d.customer_address,d.customer_mobile ");
        sb.append(" from  crm_platform_fitting_refund a");
        sb.append(" left join crm_order d on a.order_id=d.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 getOldFittingCount(Map<String, String> map) {
        StringBuffer sb = new StringBuffer();
        sb.append("select count(*) from  crm_platform_fitting_refund a");
        sb.append(" left join crm_order d on a.order_id=d.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 (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 a.fitting_version like '%" + map.get("fittingVersion").trim() + "%'");
        }
        if (StringUtils.isNotBlank(map.get("orderNumber"))) {   //工单编号
            sb.append(" and d.number like '%" + map.get("orderNumber").trim() + "%'");
        }
        if (StringUtils.isNotBlank(map.get("powerStationName"))) {  //电站名称
            sb.append(" and d.power_station_name like '%" + map.get("powerStationName").trim() + "%'");
        }
     /*   if(StringUtils.isNotBlank(map.get("fittingType"))){   //是否原配
            sb.append(" and a.fitting_type='" + map.get("fittingType") + "'");
        }*/
        if (StringUtils.isNotBlank(map.get("employeId"))) {   //服务工程师
            sb.append(" and d.employe_id='" + map.get("employeId") + "'");
        }
        if (StringUtils.isNotBlank(map.get("warrantyType"))) { //保修类型
            sb.append(" and d.warranty_type='" + map.get("warrantyType") + "'");
        }
        if (StringUtils.isNotBlank(map.get("status"))) {  //旧件状态
            sb.append(" and a.status='" + map.get("status") + "'");
        }

        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("startBackTime"))) { //返厂时间
            sb.append(" and a.send_time >= '" + map.get("startBackTime") + " 00:00:00'");
        }
        if (StringUtils.isNotBlank(map.get("endBackTime"))) {
            sb.append(" and a.send_time <= '" + map.get("endBackTime") + " 23:59:59'");
        }

        if (StringUtils.isNotBlank(map.get("startArriveTime"))) {  //到厂时间
            sb.append(" and a.end_time >= '" + map.get("startArriveTime") + " 00:00:00'");
        }
        if (StringUtils.isNotBlank(map.get("endArriveTime"))) {
            sb.append(" and a.end_time <= '" + map.get("endArriveTime") + " 23:59:59'");
        }
        return sb.toString();
    }
}
