package com.yihu.jw.dict.service;

import com.yihu.jw.dict.dao.BaseFollowUpDictDao;
import com.yihu.jw.dict.dao.BaseTemplateDictDao;
import com.yihu.jw.entity.base.dict.BaseDrugUseDictDO;
import com.yihu.jw.entity.base.dict.BaseFollowUpDictDO;
import com.yihu.jw.entity.base.dict.BaseTemplateDictDO;
import com.yihu.jw.entity.hospital.manage.BaseDiseaseHomepageBannerDO;
import com.yihu.jw.hospital.dict.BaseDiseaseHomepageBannerDao;
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.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import static com.yihu.jw.util.common.BeanUtils.getNullPropertyNames;

@Service
public class FollowUpDictService extends BaseJpaService<BaseFollowUpDictDO, BaseFollowUpDictDao> {
    @Autowired
    private BaseFollowUpDictDao baseFollowUpDictDao;
    @Autowired
    private BaseTemplateDictDao baseTemplateDictDao;
    @Autowired
    private HibenateUtils hibenateUtils;

    public void saveFollowUp(BaseFollowUpDictDO baseFollowUpDictDO) throws  Exception{
        BaseFollowUpDictDO baseDrugUseDictDO1=baseFollowUpDictDao.findByCode(baseFollowUpDictDO.getCode());
        if (baseDrugUseDictDO1!=null&&StringUtils.isNotBlank(baseDrugUseDictDO1.getId())){
            if (baseDrugUseDictDO1!=null){
                throw  new Exception("该随访code已存在");
            }
            BaseFollowUpDictDO oldfollow= baseFollowUpDictDao.findOne(baseFollowUpDictDO.getId());
            BeanUtils.copyProperties(baseFollowUpDictDO, oldfollow, getNullPropertyNames(baseFollowUpDictDO));
            baseFollowUpDictDao.save(oldfollow);
        }else {
            if (baseDrugUseDictDO1!=null){
                if (!baseDrugUseDictDO1.getId().equalsIgnoreCase(baseFollowUpDictDO.getId())){
                    throw  new Exception("该随访code已存在");
                }
            }
            baseFollowUpDictDO.setCreateTime(new Date());
            baseFollowUpDictDO.setIsDel("1");
            baseFollowUpDictDao.save(baseFollowUpDictDO);
        }
    }

    public MixEnvelop findList(String name, String status, Integer page , Integer pageSize){
        String sql="select t.id as \"id\"," +
                " t.code as \"code\"," +
                " t.name as \"name\"," +
                " t.is_required as \"isRequired\"," +
                " t.project_content as \"project_content\"," +
                " t.status as \"status\"," +
                " t.is_del as \"is_del\"," +
                " t.create_time as \"create_time\"," +
                " t.create_user as \"create_user\"," +
                " t.create_user_name as \"create_user_name\"" +
                "from base_follow_up_dict t " +
                " where t.is_del='1'";
        String sqlCount =" select count(1) as \"count\" from base_follow_up_dict t where t.is_del='1'";
        if (StringUtils.isNoneBlank(name)){
            sql+=" and t.name like '%"+name+"%'";
            sqlCount+=" and t.name like '%"+name+"%'";
        }
        if (StringUtils.isNoneBlank(status)){
            sql+=" and t.status ='"+status+"'";
            sqlCount+=" and t.status ='"+status+"'";
        }
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
        for (Map<String,Object> map:list){
            if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                map.put("statusName","生效中");
            }else {
                map.put("statusName","已失效");
            }
            if (map.get("isRequired")!=null&&"1".equalsIgnoreCase(map.get("isRequired").toString())){
                map.put("requiredName","必填");
            }else {
                map.put("requiredName","非必填");
            }
        }
        List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
        Integer count=0;
        if (countList!=null&&countList.size()>0) {
            count = Integer.valueOf(countList.get(0).get("count").toString());
        }
        MixEnvelop mixEnvelop = new MixEnvelop();
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setTotalCount(count);
        return mixEnvelop;
    }
    public BaseFollowUpDictDO findOneDict(String dictId){
        return baseFollowUpDictDao.findOne(dictId);
    }


    public void updateFollowUpStatus(String id,String status,String del) throws  Exception{
        BaseFollowUpDictDO baseFollowUpDictDO = baseFollowUpDictDao.findOne(id);
        if (baseFollowUpDictDO!=null){
            if (StringUtils.isNoneBlank(status)){
                baseFollowUpDictDO.setStatus(status);
            }
            if (StringUtils.isNoneBlank(del)){
                baseFollowUpDictDO.setIsDel(del);
                List<BaseTemplateDictDO> baseTemplateDictDO = baseTemplateDictDao.findByDictId(id);
                if (baseTemplateDictDO!=null&&baseTemplateDictDO.size()>0){
                    throw  new Exception("该使用方式已存在现有模板之中不允许删除");
                }
            }
            baseFollowUpDictDao.save(baseFollowUpDictDO);
        }
    }

}
