package com.yihu.jw.hospital.dict.service;

import com.yihu.jw.entity.base.dict.BaseIncentiveDictDO;
import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
import com.yihu.jw.hospital.dict.BaseIncentiveDictDao;
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
import com.yihu.jw.restmodel.web.ListEnvelop;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.index.qual.SearchIndexBottom;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class BaseIncentiveDictService extends BaseJpaService<BaseIncentiveDictDO, BaseIncentiveDictDao> {
    @Autowired
    private BaseIncentiveDictDao baseIncentiveDictDao;
    @Autowired
    private HibenateUtils hibenateUtils;
    @Autowired
    private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;
    /*
    * 查询激励词典
    * */
    public MixEnvelop findByName(String name, Integer page , Integer pageSize){
        MixEnvelop mixEnvelop = new MixEnvelop();
        String sql=" select t.id as \"id\"," +
                " t.incentive_name as \"incentiveName\"," +
                " t.incentive_img as \"incentiveImg\"," +
                " t.is_del as \"isDel\"," +
                " t.incentive_status as \"incentiveStatus\"," +
                " t.incentive_code as \"incentiveCode\""+
                " from base_incentive_dict t where 1=1 and t.is_del = '1' ";
        if (StringUtils.isNoneBlank(name)){
            sql+=" and t.incentive_name like '%"+name+"%'";
        }
        List<Map<String,Object>> list = hibenateUtils.createSQLQuery(sql,page,pageSize);
        List<WlyyHospitalSysDictDO> sysDictDOS=wlyyHospitalSysDictDao.findByDictName("drugStoreStatus");
        if (sysDictDOS!=null&&list.size()>0){
            for (Map<String,Object> map:list){
                for (WlyyHospitalSysDictDO sysDictDO:sysDictDOS){
                    if (map.get("incentiveStatus").toString().equalsIgnoreCase(sysDictDO.getDictCode())){
                        map.put("incentiveStatusName",sysDictDO.getDictValue());
                    }
                }
            }
        }
        List<Map<String,Object>> listCount = hibenateUtils.createSQLQuery(sql);
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setPageSize(page);
        mixEnvelop.setTotalCount(listCount==null?0:listCount.size());
        return mixEnvelop;
    }
    /*
    * 查询单条*/
    public BaseIncentiveDictDO findOneById(String id){
        BaseIncentiveDictDO baseIncentiveDictDO = baseIncentiveDictDao.findoneById(id);
        return  baseIncentiveDictDO;
    }
    /*
    * 更新/新增*/
    public BaseIncentiveDictDO saveIncentive(String id,String code,String name ,String img ,String status){
        BaseIncentiveDictDO baseIncentiveDictDO = null;
        if(StringUtils.isNoneBlank(id)){
            baseIncentiveDictDO = baseIncentiveDictDao.findOne(id);
            baseIncentiveDictDO.setIncentiveCode(code);
            baseIncentiveDictDO.setIncentiveImg(img);
            baseIncentiveDictDO.setIncentiveStatus(status);
            baseIncentiveDictDO.setIncentiveName(name);
            baseIncentiveDictDO.setIsDel("1");
            baseIncentiveDictDao.save(baseIncentiveDictDO);
        }else {
            baseIncentiveDictDO= new BaseIncentiveDictDO();
            baseIncentiveDictDO.setIncentiveCode(code);
            baseIncentiveDictDO.setIncentiveImg(img);
            baseIncentiveDictDO.setIncentiveStatus(status);
            baseIncentiveDictDO.setIncentiveName(name);
            baseIncentiveDictDO.setIsDel("1");
            baseIncentiveDictDao.save(baseIncentiveDictDO);
        }
        return baseIncentiveDictDO;
    }
    /*
     * 删除单条*/
    public Map deleteOne(String id){
        Map<String,Object> map = new HashMap<>();
        BaseIncentiveDictDO baseIncentiveDictDO = baseIncentiveDictDao.findoneById(id);
        if (baseIncentiveDictDO!=null){
            baseIncentiveDictDO.setIsDel("0");
            baseIncentiveDictDao.save(baseIncentiveDictDO);
            map.put("status",200);
            map.put("msg","删除成功");
        }else {
            map.put("status",-1);
            map.put("msg","删除失败");
        }
        return  map;
    }
}
