package com.sifang.modules.supply.stock.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.Ascription;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.ui.Model;

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

public class MaterialUtils {

    /**
     * 获取物料单位
    */
    public static List<Record> findMaterialUnit(){
        return Db.find("SELECT* FROM crm_supply_material_unit WHERE status='1'");
    }


    /**
     * 获取外部编码
     */

    public static List<String> findQqCode(){
        return Db.query(" SELECT qq_code FROM crm_supply_material WHERE status='1' and qq_code <> ''");
    }

    /**
     * 获取物料类型
     */
    public static List<Record> findMaterialType(){
        return Db.find("SELECT* FROM crm_supply_material_type WHERE status='1'");
    }

    /**
     * 物料品牌
     */
    public static List<Record> findMaterialBrand(){
        return Db.find("select m.id, m.name from crm_supply_material_brand m where m.status='1' ");
    }

    /**
     * 获取物料仓库
     */
    public static List<Record> findMaterialWarehouse(String type,String traderId){

        if(StringUtil.isNotBlank(traderId)){
            return Db.find("SELECT* FROM crm_supply_warehouse WHERE status='0' and ascription_id=?",traderId);
        }else
        if(StringUtil.isNotBlank(type)){
            return Db.find("SELECT* FROM crm_supply_warehouse WHERE status='0' and type=?",type);
        }
        return Db.find("SELECT * FROM crm_supply_warehouse WHERE status='0' ");
    }

    public static List<String> findSupplierIdsList(String userRelId){
        return Db.query(" SELECT id FROM `crm_supply_supplier` WHERE supervision_id=? AND STATUS='0' AND binding_flag='1' ",userRelId);
    }

    public static List<Record> findMaterialWarehouseByAscIds(List<String> ascription) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT * FROM crm_supply_warehouse")
                .line("WHERE status='0'")
                .inst("and ascription_id in ",ascription)
                .line("order by create_time");
        return Db.find(sql.build(),sql.params());
     //   return Db.find("SELECT * FROM crm_supply_warehouse WHERE status='0' and ascription_id=? order by create_time ", ascriptionId);
    }

    public static List<Record> findMaterialWarehouseByAscriptionId(String ascriptionId) {
         return Db.find("SELECT * FROM crm_supply_warehouse WHERE status='0' and ascription_id=? order by create_time ", ascriptionId);
    }

    public static List<Record> findWarehouseBySecondTrader(String traderId) {
        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT w.*");
        sb.append(" FROM crm_supply_warehouse w");
        sb.append(" INNER JOIN crm_trader_warehouse_rel wr ON wr.warehouse_id = w.id ");
        sb.append(" AND wr.trader_id = ?");
        sb.append(" WHERE w.status='0'");
        sb.append(" order by w.create_time");
        return Db.find(sb.toString(), traderId);
    }


    public static List<Record> getDeliveryMethod(){
        return Db.find(" SELECT m.id,m.name,m.type FROM `crm_supply_delivery_method` m WHERE m.status='1'  ");
    }


    public static void returnModelMaterial(Model model,String type,String traderId){
        model.addAttribute("units",findMaterialUnit());
        model.addAttribute("types",findMaterialType());
        model.addAttribute("wares",findMaterialWarehouse(type,traderId));
    }

    public static List<Record> getWarehouseList(){
        User user= UserUtils.getUser();
        String userType= CrmUtils.getCurrentUserType(user);
        Ascription ascription=CrmUtils.getAscription();
        if(!"1".equals(userType) && !User.USER_TYPE_WXG.equals(userType)){
            return Db.find("SELECT id,name,type FROM crm_supply_warehouse WHERE status='0' and ascription_id=?",ascription.getAscriptionId());
        }
        return Db.find("SELECT id,name,type FROM crm_supply_warehouse WHERE status='0' ");
    }

    public static List<String> findMaterialCode(){
        return Db.query(" SELECT code FROM crm_supply_material WHERE status='1'");
    }


    public static List<String> findMaterialTypeName(){
        return Db.query("SELECT name FROM crm_supply_material_type WHERE status='1'");
    }

    public static List<String> getPlateWarehouseName(){
        return Db.query(" SELECT  name FROM `crm_supply_warehouse` where  type='1' and status='0' ");
    }

    public static List<Record> getPlateWarehouse(){
        return Db.find(" SELECT id,  name FROM `crm_supply_warehouse` where  type='1' and status='0' ");
    }

    public static List<Record> getPlateWarehouse2(){
        return Db.find(" SELECT id,  name FROM `crm_supply_warehouse` where  type !='2' and status='0' ");
    }

    public static List<String> getMaterialBrand(){
        return Db.query("select m.name from crm_supply_material_brand m where m.status='1' ");
    }


    public static List<String> findMaterialUnitName(){
        return Db.query("SELECT name FROM crm_supply_material_unit WHERE status='1'");
    }

    public static String findMaterialIdByCode(String code){
        return Db.queryStr("SELECT id FROM crm_supply_material WHERE status='1' and code=?",code);
    }

    /**
     * 获取 物料归属
    */
    public static List<Map<String,Object>> MaterialOwnership(String type){
        List<Map<String,Object>> lists = Lists.newArrayList();
        if(StringUtil.isBlank(type)){
            Map<String,Object> map = Maps.newHashMap();
            map.put("value","a");
            map.put("name","供应链");
            lists.add(map);
        }
        //获取供应商
        List<Record> tralist = Db.find(" SELECT id,name FROM  crm_supply_supplier WHERE status='0'");
        List<Map<String,Object>> tralists = Lists.newArrayList();
        for(Record rd:tralist){
            Map<String,Object> tmap = Maps.newHashMap();
            tmap.put("value",rd.getStr("id"));
            tmap.put("name",rd.getStr("name"));
            tralists.add(tmap);
        }
        Map<String,Object> tramap = Maps.newHashMap();
        tramap.put("value","b");
        tramap.put("name","供应商");
        tramap.put("children",tralists);
        lists.add(tramap);

        List<Record> supplist = Db.find(" SELECT id,name FROM  crm_trader WHERE status='0'");
        List<Map<String,Object>> sulist = Lists.newArrayList();
        for(Record rd:supplist){
            Map<String,Object> tmap = Maps.newHashMap();
            tmap.put("value",rd.getStr("id"));
            tmap.put("name",rd.getStr("name"));
            sulist.add(tmap);
        }
        Map<String,Object> sumap = Maps.newHashMap();
        sumap.put("value","c");
        sumap.put("name","代理商");
        sumap.put("children",sulist);
        lists.add(sumap);

        return lists;
    }

    public static String findSupplierNameById(String id){
        return Db.queryStr("SELECT name FROM `crm_supply_supplier` WHERE id=?",id);
    }

    //物料类型：0.标准物料 1.自选产品  用于赊销下单
    public static List<Record> findMaterialTypeForSXXD(String attribute){
        //TODO 暂时只能选择 物料类型：逆变器、并网箱、组件
        return Db.find("SELECT id,name FROM crm_supply_material_type WHERE status='1' and name in ('逆变器','并网箱','组件') and attribute=?",attribute);
    }

    public static String findSupplierBrandById(String id){
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT b.name FROM `crm_supply_supplier` a")
                .line("LEFT JOIN crm_supply_material_brand b ON b.id = a.material_brand_id")
                .line("WHERE a.id=? AND b.status = '1' ",id)
                .line("LIMIT 1");
        return Db.queryStr(sql.build(),sql.params());
    }


    public static List<Record> findMaterialTypeBz1(){
        return Db.find("SELECT id,name FROM crm_supply_material_type WHERE status='1' and attribute='0' and name<>'组件' ");
    }
    public static Record findMaterialTypeBz2(){
        return Db.findFirst("SELECT id,name FROM crm_supply_material_type WHERE status='1' and attribute='0' and name='组件' ");
    }
    public static String findQqCodeByCode(String code) {
        return Db.queryStr("SELECT id FROM crm_supply_material WHERE status='1' and qq_code=?",code);
    }

    public static List<String> findMaterialModel(){
        return Db.query("SELECT CONCAT(type, model, material_model) AS tmm FROM crm_electric_set WHERE status='0'");
    }

}
