package com.sifang.modules.supply.setting.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.setting.dao.CrmSupplyMaterialBrandDao;
import com.sifang.modules.supply.setting.dao.CrmSupplyMaterialTypeDao;
import com.sifang.modules.supply.setting.entity.CrmSupplyMaterialBrand;
import com.sifang.modules.supply.setting.entity.CrmSupplyMaterialType;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.util.LayuiPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class CrmSupplyMaterialBrandService extends BaseService {

    @Autowired
    private CrmSupplyMaterialBrandDao brandDao;

    @Autowired
    private CrmSupplyMaterialTypeDao typeDao;

    private Logger logger = LoggerFactory.getLogger(getClass());
    public LayuiPage materialBrandList(LayuiPage page, Map<String, String> map) {
        List<Record> list = brandDao.materialBrandList(page, map);
        page.setData(list);
        page.setCount(brandDao.materialBrandListCount(map));
        return page;
    }

    public void saveMaterialBrand(String name,String id, String typeId) {
        if (StringUtil.isBlank(typeId)) {
            throw new BusinessException("物料类型不可为空!");
        }
        // 查询物料类型
        CrmSupplyMaterialType materialType = typeDao.get(typeId);
        if (materialType == null) {
            throw new BusinessException("未查询到物料类型信息!");
        }

        if (brandDao.checkName(name, id, typeId)) {
            throw new BusinessException("该类型的物料品牌已存在，请勿重复添加!");
        }
        Date date = new Date();
        User user = UserUtils.getUser();
        CrmSupplyMaterialBrand materialBrand;
        if (StringUtil.isBlank(id)) {
            materialBrand = new CrmSupplyMaterialBrand();
            materialBrand.setCreateTime(date);
            materialBrand.setCreateBy(user.getId());
        } else {
            materialBrand = brandDao.get(id);
            materialBrand.setUpdateTime(date);
        }
        materialBrand.setTypeId(materialType.getId());
        materialBrand.setName(name);
        brandDao.save(materialBrand);
    }

    public CrmSupplyMaterialBrand getByBrandId(String id){
        return brandDao.get(id);
    }

    public void deleteMaterialBrand(String ids) {
        Db.update("UPDATE `crm_supply_material_brand` SET STATUS='2',update_time=NOW() WHERE id in (" + StringUtil.joinInSql(ids.split(",")) + ")");
    }


    //获取所有物料品牌
    public List<Record> getAllMaterialBrand(){
        return brandDao.getAllMaterialBrand(null);
    }

    // 根据物料类型查询品牌
    public List<Record> getListByTypeId(String typeId) {
        return brandDao.getAllMaterialBrand(typeId);
    }

}
