package com.sifang.modules.supply.order.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.order.entity.CrmSupplyMaterialReturnOrder;
import com.sifang.modules.sys.common.persistence.BaseDao;
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 MaterialReturnOrderDao extends BaseDao<CrmSupplyMaterialReturnOrder> {

    //平台退货页面数据
    public List<Record> getList(LayuiPage<Record> page, Map<String, String> map) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.line("SELECT o.id,o.number,o.status,o.type,o.relation_number,o.relation_id,o.trader_name,o.warehouse_name,o.return_method_name")
                .line(",o.express_no,o.return_driver,o.return_mobile,o.return_car_num,o.return_remark,o.create_time,d.deliver_name,s.name as supplierName")
                .line("FROM crm_supply_material_return_order o")
                .line("left join crm_supply_material_deliver_order d on d.id=o.relation_id ")
                .line("left join crm_supply_supplier s on s.id = o.supply_id")
                .line("WHERE o.status IN ('1','2','3')");

        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 countList(Map<String, String> map) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.line(" SELECT count(o.id) ")
                .line(" FROM `crm_supply_material_return_order` o")
                .line("WHERE o.status IN ('1','2','3')");

        appendCondition(map, sqlBuilder);
        return Db.queryLong(sqlBuilder.build(), sqlBuilder.params());
    }

    //代理商退货页面数据
    public List<Record> getTraderReturnList(LayuiPage<Record> page, Map<String, String> map, String traderId) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.line("SELECT o.id,o.number,o.status,o.type,o.relation_number,o.relation_id,o.trader_name,o.warehouse_name,o.return_method_name")
                .line(",o.express_no,o.return_driver,o.return_mobile,o.return_car_num,o.return_remark,o.create_time,o.cancel_reason,o.cancel_time")
                .line("FROM crm_supply_material_return_order o")
                .line("WHERE o.status IN ('1','2','3') and o.type in ('1','2') and o.trader_id = ?", traderId);

        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 traderReturnCountList(Map<String, String> map, String traderId) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.line(" SELECT count(o.id) ")
                .line(" FROM `crm_supply_material_return_order` o")
                .line("WHERE o.status IN ('1','2','3') and o.type in ('1','2') and o.trader_id = ?", traderId);

        appendCondition(map, sqlBuilder);
        return Db.queryLong(sqlBuilder.build(), sqlBuilder.params());
    }


    //供应商退货页面数据
    public List<Record> getSupplyReturnList(LayuiPage<Record> page, Map<String, String> map, String supplyId) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.line("SELECT o.id,o.number,o.status,o.type,o.relation_number,o.relation_id,o.trader_name,o.warehouse_name,o.return_method_name")
                .line(",o.express_no,o.return_driver,o.return_mobile,o.return_car_num,o.return_remark,o.create_time")
                .line(",o.person_in_charge,o.person_mobile,o.warehouse_address,o.deliver_name")
                .line("FROM crm_supply_material_return_order o")
                .line("WHERE o.status IN ('1','2','3') and o.type = '3' and o.supply_id = ?", supplyId);

        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 supplyReturnCountList(Map<String, String> map, String supplyId) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.line(" SELECT count(o.id) ")
                .line(" FROM `crm_supply_material_return_order` o")
                .line("WHERE o.status IN ('1','2','3') and o.type = '3' and o.supply_id = ?", supplyId);

        appendCondition(map, sqlBuilder);
        return Db.queryLong(sqlBuilder.build(), sqlBuilder.params());
    }


    public void appendCondition(Map<String, String> map, SqlBuilder builder) {
        //退货单号
        if (StringUtil.checkParamsValid(map.get("number"))) {
            builder.line("and o.number like concat('%', ?, '%')", map.get("number").trim());
        }
        //退货状态
        if (StringUtil.checkParamsValid(map.get("status"))) {
            builder.line("and o.status = ?", map.get("status").trim());
        }
        //退货订单
        if (StringUtil.checkParamsValid(map.get("relationNumber"))) {
            builder.line("and o.relation_number like concat('%', ?, '%')", map.get("relationNumber").trim());
        }
        //退货备注
        if (StringUtil.checkParamsValid(map.get("returnRemark"))) {
            builder.line("and o.return_remark like concat('%', ?, '%')", map.get("returnRemark").trim());
        }
        //代理商名称
        if (StringUtil.checkParamsValid(map.get("traderId"))) {
            builder.line("and o.trader_id = ?", map.get("traderId").trim());
        }
        //退货方式
        if (StringUtil.checkParamsValid(map.get("returnMethodId"))) {
            builder.line("and o.return_method_id = ?", map.get("returnMethodId").trim());
        }
        //物流单号
        if (StringUtil.checkParamsValid(map.get("expressNo"))) {
            builder.line("and o.express_no like concat('%', ?, '%') ", map.get("expressNo").trim());
        }
        //退货时间
        if (StringUtil.checkParamsValid(map.get("createTimeMin"))) {
            builder.line("and o.create_time >= ?", map.get("createTimeMin"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMax"))) {
            builder.line("and o.create_time <= ?", map.get("createTimeMax"));
        }
    }


    public Record findOrderById(String id) {
        return Db.findFirst("select * from crm_supply_material_return_order where id=?", id);
    }
}
