package com.bzd.system.service.spgl;

import com.bzd.common.config.dao.DaoBase;
import com.bzd.common.config.dao.DaoSupport;
import com.bzd.common.config.dao.PageData;
import com.bzd.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.bzd.common.utils.ShiroUtils.getSysUser;


/**
*
* 门店积分商品列表 service
* @author LiXiangFei
* @since 2024-10-15
*/

@Service
public class SPProductinfoService {

    @Autowired
    private DaoBase dao;

    @Resource(name = "daoSupport")
    private DaoSupport daoSupport;

    @Transactional
    public Integer productAdd(PageData pd)throws Exception {
        Integer i = productAdd2(pd);
        Integer i3 = productAdd3(pd);
        Integer i4 = productAdd4(pd);
        Integer i5 = productAdd5(pd);
        Integer i6 = productAdd6(pd);
        Integer i12 = productAdd12(pd);
        if (i!=1||i3!=1||i4!=1||i5!=1||i6!=1||i12!=1){
            return 0;
        }
        return daoSupport.update("SPProductinfoMapper.addProductAdd", pd);
    }

    @Transactional
    public Integer ProductUpdate(PageData pd)throws Exception {
        //查询
        List<PageData> pageData = (List<PageData>) daoSupport.findForList("SPProductinfoMapper.findSPProductinfoCode", pd);

        if(!pageData.get(0).get("code3").toString().equals("0")){
            Integer i = daoSupport.update("SPProductinfoMapper.productUpdate2", pd);
        }else {
            productAdd2(pd);
        }
        if(!pageData.get(0).get("code4").toString().equals("0")) {
            Integer i3 = daoSupport.update("SPProductinfoMapper.productUpdate3", pd);
        }else {
            productAdd3(pd);
        }
        if(!pageData.get(0).get("code5").toString().equals("0")) {
            Integer i4 = daoSupport.update("SPProductinfoMapper.productUpdate4", pd);
        }else {
            productAdd4(pd);
        }
        if(!pageData.get(0).get("code6").toString().equals("0")) {
            Integer i5 = daoSupport.update("SPProductinfoMapper.productUpdate5", pd);
        }else {
            productAdd5(pd);
        }
        if(!pageData.get(0).get("code7").toString().equals("0")) {
            Integer i6 = daoSupport.update("SPProductinfoMapper.productUpdate6", pd);
        }else {
            productAdd6(pd);
        }
        if(!pageData.get(0).get("code12").toString().equals("0")) {
            Integer i12 = daoSupport.update("SPProductinfoMapper.productUpdate12", pd);
        }else {
            productAdd12(pd);
        }
        //这里同步修改管理品的数据
        PageData pdConfig=new PageData();
        //更新患者管理品
        pdConfig.put("updatedBy", getSysUser().getUserId());
        pdConfig.put("updatedTime", DateUtils.getTime());
        pdConfig.put("mdmCode",pd.get("product_code"));
        pdConfig.put("administrationMethod",pd.get("drug_delivery_way"));
        pdConfig.put("isRegisteredManaged",pd.get("registered_item"));
        pdConfig.put("isFollowUpManaged",pd.get("follow_up_item"));
        pdConfig.put("isColdChainManaged",pd.get("cold_chain_item"));
        //pdConfig.put("enterpriseFlowManagement",pd.get("flow_item"));//暂时不考虑这值 待确认
        pdConfig.put("isCharityAidManaged",pd.get("charity_aid_item"));
        pdConfig.put("dosage_max",pd.get("dosage_max"));
        pdConfig.put("dosage_min",pd.get("dosage_min"));
        pdConfig.put("dosage_normal",pd.get("dosage_normal"));
        pdConfig.put("unit_normal",pd.get("unit_normal"));
        pdConfig.put("unit_max",pd.get("unit_max"));
        pdConfig.put("unit_min",pd.get("unit_min"));
        pdConfig.put("packing_unit",pd.get("packing_unit"));
        pdConfig.put("packing_value",pd.get("packing_value"));
        pdConfig.put("productName",pd.get("product_name"));
        pdConfig.put("genericName",pd.get("generic_name"));
        pdConfig.put("manufacturerShortName",pd.get("manufacturer_abbreviation"));//厂家简称
        System.out.println("pdConfig患者管理品:"+pdConfig);
        int result= daoSupport.update("drugConfigMapper.updateDrugConfig2", pdConfig);
        System.out.println("result:这里同步修改管理品的数据s_gxhpz_drugconfig表更改条数"+result);
        return daoSupport.update("SPProductinfoMapper.productUpdate", pd);
    }
    public Integer productAdd2(PageData pd)throws Exception {
        return daoSupport.update("SPProductinfoMapper.addProductAdd2", pd);
    }
    public Integer productAdd3(PageData pd)throws Exception {
        return daoSupport.update("SPProductinfoMapper.addProductAdd3", pd);
    }
    public Integer productAdd4(PageData pd)throws Exception {
        return daoSupport.update("SPProductinfoMapper.addProductAdd4", pd);
    }
    public Integer productAdd5(PageData pd)throws Exception {
        return daoSupport.update("SPProductinfoMapper.addProductAdd5", pd);
    }
    public Integer productAdd6(PageData pd)throws Exception {
        return daoSupport.update("SPProductinfoMapper.addProductAdd6", pd);
    }

    public Integer productAdd12(PageData pd)throws Exception {


        return daoSupport.update("SPProductinfoMapper.addProductAdd12", pd);
    }

    public List<PageData> findSPProductinfoList(PageData pd) throws Exception{
        return (List<PageData>) daoSupport.findForList("SPProductinfoMapper.findSPProductinfoList", pd);
    }
    public List<PageData> findSPProductinfoListBYProductCode(PageData pd) throws Exception{
        return (List<PageData>) daoSupport.findForList("SPProductinfoMapper.findSPProductinfoListBYProductCode", pd);
    }

    public Integer SPProductinfoRemove(PageData pd)throws Exception {
        // 查询数据 code
        List<PageData> codeList = (List<PageData>) daoSupport.findForList("SPProductinfoMapper.findSPProductinfoProductCode", pd);
        if(codeList.size()>0){
            if (codeList.get(0).get("product_code")!=null){
                pd.put("product_code",codeList.get(0).get("product_code"));
                daoSupport.delete("SPProductinfoMapper.deleteProductByCode", pd);
                daoSupport.delete("SPProductinfoMapper.deleteProductInformationByCode", pd);
                daoSupport.delete("SPProductinfoMapper.deleteProductDetailsByCode", pd);
                daoSupport.delete("SPProductinfoMapper.deleteProductClassificationByCode", pd);
                daoSupport.delete("SPProductinfoMapper.deleteProductAttributesByCode", pd);
                daoSupport.delete("SPProductinfoMapper.deleteProductDtpinfoByCode", pd);
            }
        }
        return daoSupport.delete("SPProductinfoMapper.SPProductinfoRemove", pd);
    }


    public Integer updateSSpglJfspProductinfo(PageData pd)throws Exception {
        return daoSupport.update("SSpglJfspProductinfoMapper.updateSSpglJfspProductinfo", pd);
    }

    public PageData findAllList(PageData pd) throws Exception{
        List<PageData> codeList = (List<PageData>) daoSupport.findForList("SPProductinfoMapper.findSPProductinfoProductCode", pd);
        if(codeList.size()>0){
            if (codeList.get(0).get("product_code")!=null){
                pd.put("product_code",codeList.get(0).get("product_code"));
                List<PageData> list = (List<PageData>) daoSupport.findForList("SPProductinfoMapper.findAllList", pd);
                return list.get(0);
            }
        }
        return null;
    }
}
