package com.bzd.system.service.gxhpz;

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 com.bzd.common.utils.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
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 java.util.Map;

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

@Service
public class DrugConfigService {
    @Autowired
    private DaoBase dao;

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

    /**
     * 查询列表
     * @param pd
     * @return
     * @throws Exception
     */
    public List<PageData> findForList(final PageData pd) throws Exception {
        return (List<PageData>) daoSupport.findForList("drugConfigMapper.selectDrugConfig", pd);
    }
    public List<PageData> selectproductByCodeAndName(final PageData pd) throws Exception {
        return    (List<PageData>) daoSupport.findForList("drugConfigMapper.selectproductByCodeAndName", pd);
    }
    public PageData selectOneproductByCodeAndName(final PageData pd) throws Exception {
        return    (PageData) daoSupport.findForObject("drugConfigMapper.selectOneproductByCodeAndName", pd);
    }

    /**
     * 查询单个
     * @param pd
     * @return
     * @throws Exception
     */
    public Object findOne(final PageData pd) throws Exception {
        return daoSupport.findForObject("drugConfigMapper.selectOne", pd);
    }

    /**
     * 添加
     * @param pd
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public int save(final PageData pd) throws Exception {
        pd.put("createdBy", getSysUser().getUserId());
        pd.put("createdTime", DateUtils.getTime());
        //添加管理品时候同步总药库库信息
        PageData pd12=new PageData();
        pd12.put("drug_delivery_way", pd.get("administrationMethod"));//给药方式
        pd12.put("registered_item", pd.get("isRegisteredManaged"));//登记品
        pd12.put("follow_up_item", pd.get("isFollowUpManaged"));//随访品
        pd12.put("cold_chain_item", pd.get("isColdChainManaged"));//冷链品
        //pd12.put("flow_item", pd.get("enterpriseFlowManagement"));//流向品  待确认是否修改
        pd12.put("charity_aid_item", pd.get("isCharityAidManaged"));//慈善援助品
        pd12.put("product_code", pd.get("mdmCode"));//药品编码
        pd12.put("dosage_max",pd.get("dosage_max"));
        pd12.put("dosage_min",pd.get("dosage_min"));
        pd12.put("dosage_normal",pd.get("dosage_normal"));
        pd12.put("unit_normal",pd.get("unit_normal"));
        pd12.put("unit_max",pd.get("unit_max"));
        pd12.put("unit_min",pd.get("unit_min"));
        pd12.put("packing_unit",pd.get("packing_unit"));
        pd12.put("packing_value",pd.get("packing_value"));
        int res12=daoSupport.update("SPProductinfoMapper.productUpdate12", pd12);
        System.out.println("res12:s_gxhpz_product_dtpinfo表更改条数"+res12);
        //添加管理品时候同步总药库库信息
        PageData pd13=new PageData();
        pd13.put("product_code", pd.get("mdmCode"));//药品编码
        pd13.put("product_name", pd.get("productName"));//通用名
        pd13.put("generic_name", pd.get("genericName"));//通用名
        pd13.put("manufacturer_abbreviation", pd.get("manufacturerShortName"));//厂家简称
        int res13=daoSupport.update("SPProductinfoMapper.productUpdate", pd13);
        System.out.println("res13:s_gxhpz_product_info表更改条数"+res13);
        //添加管理品
        return daoSupport.save("drugConfigMapper.addDrugConfig", pd);
    }

    /**
     * 删除
     * @param ids
     * @return
     * @throws Exception
     */
    public Integer del(List<String> ids) throws Exception {
        return daoSupport.delete("drugConfigMapper.deleteDrugConfigsByIds", ids);
    }

    /**
     * 修改
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer update(PageData pd) throws Exception {
        String type = (String) pd.get("type");
        //type=1的时候在配置D值品，需要把新增的D制品名称和编码存入管理品表 条件是根据mdmCode修改
        //type=0的时候修改所有字段 条件是根据id修改
        if(type.equals("1")){
            int result1=0;
            String dValueId = (String) pd.get("dValueId");
            String dValueName = (String) pd.get("dValueName");
            Object drugData =pd.get("drugData");
            if(StringUtils.isNotNull(drugData)){
                try {
                    // 将 JSON 字符串转换为 List<Map<String, Object>>
                    ObjectMapper objectMapper = new ObjectMapper();
                    List<Map<String, Object>> rowsList = objectMapper.readValue(drugData.toString(), List.class);
                    // 遍历列表并调用插入数据接口
                    for (Map<String, Object> hbs : rowsList) {
                        PageData pd4=new PageData();
                        pd4.put("d_value_code", dValueId);
                        pd4.put("dValueName", dValueName);
                        pd4.put("mdmCode", hbs.get("mdmCode"));
                        pd4.put("dvalueStatus", pd.get("saveStatus"));
//                        pd4.put("specification", hbs.get("specification"));
//                        pd4.put("manufacturerShortName", hbs.get("manufacturerShortName"));
//                        pd4.put("isFollowUpManaged", hbs.get("isFollowUpManaged"));
//                        pd4.put("isColdChainManaged", hbs.get("isColdChainManaged"));
//                        pd4.put("isRegisteredManaged", hbs.get("isRegisteredManaged"));
//                        pd4.put("isCharityAidManaged", hbs.get("isCharityAidManaged"));
//                        pd4.put("administrationMethod", hbs.get("administrationMethod"));
//                        pd4.put("dosageAndAdministration", hbs.get("dosageAndAdministration"));
//                        pd4.put("enterpriseFlowManagement", hbs.get("enterpriseFlowManagement"));
                        //pd4.put("createdBy", pd.get("createdBy"));
                        //pd4.put("createdTime", pd.get("createdTime"));
                        pd4.put("dvupdatedby", getSysUser().getUserId());
                        pd4.put("dvcreatedTime", DateUtils.getTime());
                        pd4.put("remarks", pd.get("remarks"));
                        result1 = daoSupport.update("drugConfigMapper.updateDrugConfig2", pd4);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(result1>0 ){
                return 1;
            }else{
                return 0;
            }
        }else{
            //更新管理品时候同步总药库库信息s_gxhpz_product_dtpinfo表
            PageData pd12=new PageData();
            pd12.put("drug_delivery_way", pd.get("administrationMethod"));//给药方式
            pd12.put("registered_item", pd.get("isRegisteredManaged"));//登记品
            pd12.put("follow_up_item", pd.get("isFollowUpManaged"));//随访品
            pd12.put("cold_chain_item", pd.get("isColdChainManaged"));//冷链品
            //pd12.put("flow_item", pd.get("enterpriseFlowManagement"));//流向品 待确认是否修改
            pd12.put("charity_aid_item", pd.get("isCharityAidManaged"));//慈善援助品
            pd12.put("product_code", pd.get("mdmCode"));//药品编码
            pd12.put("dosage_max",pd.get("dosage_max"));
            pd12.put("dosage_min",pd.get("dosage_min"));
            pd12.put("dosage_normal",pd.get("dosage_normal"));
            pd12.put("unit_normal",pd.get("unit_normal"));
            pd12.put("unit_max",pd.get("unit_max"));
            pd12.put("unit_min",pd.get("unit_min"));
            pd12.put("packing_unit",pd.get("packing_unit"));
            pd12.put("packing_value",pd.get("packing_value"));
            int res12=daoSupport.update("SPProductinfoMapper.productUpdate12", pd12);
            System.out.println("res12:s_gxhpz_product_dtpinfo表更改条数"+res12);
            //更新管理品时候同步总药库库信息s_gxhpz_product_info表
            PageData pd13=new PageData();
            pd13.put("product_code", pd.get("mdmCode"));//药品编码
            pd13.put("product_name", pd.get("productName"));//通用名
            pd13.put("generic_name", pd.get("genericName"));//通用名
            pd13.put("manufacturer_abbreviation", pd.get("manufacturerShortName"));//厂家简称
            int res13=daoSupport.update("SPProductinfoMapper.productUpdate", pd13);
            System.out.println("res13:s_gxhpz_product_info表更改条数"+res13);
            //更新患者管理品
            pd.put("updatedBy", getSysUser().getUserId());
            pd.put("updatedTime", DateUtils.getTime());
            return daoSupport.update("drugConfigMapper.updateDrugConfig", pd);

        }
    }

    /**
     * 启用停用配置
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer changeStatus(PageData pd) throws Exception {
        pd.put("updatedTime", DateUtils.getTime());
        pd.put("updatedBy", getSysUser().getUserId());
        return daoSupport.update("drugConfigMapper.changeStatus", pd);
    }
    /**配置药查询**/
    public List<PageData> getAllDrugs(final PageData pd) throws Exception {
        return (List<PageData>) daoSupport.findForList("drugConfigMapper.getAllDrugConfig", pd);
    }
    /**配置药查询**/
    public List<PageData> getAllProductInfo(final PageData pd) throws Exception {
        return (List<PageData>) daoSupport.findForList("drugConfigMapper.getAllProductInfo", pd);
    }

    /**
     * 启用停用D值配关联
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer changedvalueStatus(PageData pd) throws Exception {
        pd.put("updatedTime", DateUtils.getTime());
        pd.put("updatedBy", getSysUser().getUserId());
        return daoSupport.update("drugConfigMapper.changedvalueStatus", pd);
    }

}
