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 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
public class DvalueConfigService {
    @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("dvalueMapper.selectDValue", pd);
    }

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

    /**
     * 添加
     * @param pd
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public int save(final PageData pd) throws Exception {
        Object o=daoSupport.findForObject("dvalueMapper.checkValueByCode", pd);
        if(o!=null){
            return -1;
        }else {
            return  daoSupport.save("dvalueMapper.insertDValue", pd);
        }

    }

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

    /**
     * 修改配置
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer update(PageData pd) throws Exception {
        return daoSupport.update("dvalueMapper.updateDValue", pd);
    }

    /**
     * 启用停用D值配置
     * @param pd
     * @return
     * @throws Exception
     */
    public String changeStatus(PageData pd) throws Exception {
        pd.put("updatedTime", DateUtils.getTime());
        pd.put("updatedBy", getSysUser().getUserId());
        PageData pdd= (PageData) daoSupport.findForObject("dvalueMapper.selectOneById", pd);
        Long d_value_code= (Long) pdd.get("d_value_code");
        PageData pd1 = new PageData();
        pd1.put("d_value_code",d_value_code);
        List<PageData> list= (List<PageData>) daoSupport.findForList("drugConfigMapper.selectDrugConfigByd_value_code", pd1);
        if(list.size()>0){
            return "有绑定药品不能停用!";
        }else {
          int result=  daoSupport.update("dvalueMapper.changeStatus", pd);
          if(result>0){
              return "success";
          }else{
              return "error";
          }
        }
    }


}
