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.CrmSiteFitting;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.util.LayuiPage;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Repository
public class CrmSiteFittingDao extends BaseDao<CrmSiteFitting> {


    public CrmSiteFitting getByFittingCode(String fittingCode,String siteId){
        return getByHql("from CrmSiteFitting where code = :p1 and status ='1' and siteId =:p2 ",new Parameter(fittingCode,siteId));
    }

    public List<CrmSiteFitting> getByPlatformFittingId(String id){
        String sql = "from CrmSiteFitting where platformFittingId = :id and status ='1'";

            Query query = getSession().createQuery(sql);
                query.setParameter("id", id);
            List<CrmSiteFitting> list = query.list();

        return list;

    }

    public List<Record> getFittingList(LayuiPage page, Map<String,String> map,String siteId){

        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,s.name as site_name,ifnull((select sum(fitting_apply_num) from crm_site_fitting_apply  fa where fa.fitting_id = a.id and fa.status='0' ),0) as num FROM crm_site_fitting a left join crm_site s on s.id=a.site_id  WHERE a.status='1'  ");
        if(StringUtil.isNotBlank(siteId)){
            sf.append("and a.site_id = '" + siteId + "' ");
        }

        sf.append(search(map));
        sf.append(" order by a.create_time desc ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public long getFittingCount(Map<String,String> map,String siteId){
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(id) FROM crm_site_fitting a WHERE a.status='1' ");
        if(StringUtil.isNotBlank(siteId)){
            sf.append("and a.site_id = '" + siteId + "' ");
        }
        sf.append(search(map));
        return Db.queryLong(sf.toString());
    }

    public List<Record> getPlatformFittingList(LayuiPage page, Map<String,String> map){
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT * FROM crm_platform_fitting a WHERE a.status='1' ");
        sf.append(search(map));
        sf.append(" order by a.create_time desc ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public long getPlatformFittingCount(Map<String,String> map){
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(id) FROM crm_platform_fitting a WHERE a.status='1' ");
        sf.append(search(map));
        return Db.queryLong(sf.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.code like '%" + map.get("fittingCode").trim() + "%' ");
            }
            if (StringUtil.checkParamsValid(map.get("fittingName"))) {
                sb.append(" and a.name like '%" + map.get("fittingName").trim() + "%' ");
            }
            if (StringUtil.checkParamsValid(map.get("fittingVersion"))) {
                sb.append(" and a.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("oldRetrun"))) {
                sb.append(" and a.refund_old_flag = '" + map.get("oldRetrun").trim() + "' ");
            }
            if(StringUtil.checkParamsValid(map.get("remarks"))){
                sb.append(" and a.remarks like '%" + map.get("remarks")+ "%'");
            }
            if(StringUtil.checkParamsValid( map.get("warningMin"))){
                sb.append(" and a.warning >= " + new BigDecimal(map.get("warningMin")));
            }
            if(StringUtil.checkParamsValid( map.get("warningMax"))){
                sb.append(" and a.warning <= " + new BigDecimal(map.get("warningMax")));
            }
        }
        return sb.toString();
    }

    public String getLocation(String id,String siteId) {
        String s = "select fitting_code from crm_platform_fitting_apply where id=?";
        Record r = Db.findFirst(s, id);
        String code = r.getStr("fitting_code");
        String sql = "select location from crm_site_fitting where code =? and site_id=? and status='1'";
        Record record = Db.findFirst(sql, code, siteId);
        if (record != null) {
            return record.getStr("location");
        } else {
            return "";
        }
    }
}
