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

import com.yihu.jw.entity.hospital.message.BaseBannerDO;
import com.yihu.jw.entity.hospital.suggest.BaseSuggestionDO;
import com.yihu.jw.entity.hospital.suggest.BaseSuggestionDictDO;
import com.yihu.jw.hospital.suggest.dao.BaseSuggestionDictDao;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.util.date.DateUtil;
import com.yihu.jw.utils.StringUtil;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class BaseSuggestionDictService extends BaseJpaService<BaseSuggestionDictDO, BaseSuggestionDictDao> {
    @Autowired
    private BaseSuggestionDictDao baseSuggestionDictDao;
    @Autowired
    private HibenateUtils hibenateUtils;

    /**
     * 建议类型上移
     * @param
     * @return
     * @throws Exception
     */
    public BaseSuggestionDictDO upSuggestion(String id) throws Exception{
        BaseSuggestionDictDO baseSuggestionDictDO = baseSuggestionDictDao.findOne(id);
        int maxSort = baseSuggestionDictDao.getMaxSort();
        int sort = 0;
        if(null!=baseSuggestionDictDO){
            sort = baseSuggestionDictDO.getIsSort();
        }
        if (maxSort==sort){
            throw new Exception("不能上移");
        }
        String sql = "select t.id AS \"id\" from base_suggestions_dict t where t.is_sort > "+sort+" order by t.is_sort asc ";
        System.out.println(sql);
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,1,1);
        BaseSuggestionDictDO upPrevious = new BaseSuggestionDictDO();
        if (list.size()>0){
            String prviousBannerId = list.get(0).get("id").toString();
            upPrevious =baseSuggestionDictDao.findOne(prviousBannerId);
        }
        //交换sort值
        baseSuggestionDictDO.setIsSort(upPrevious.getIsSort());
        upPrevious.setIsSort(sort);
        baseSuggestionDictDao.save(baseSuggestionDictDO);
        baseSuggestionDictDao.save(upPrevious);
        return baseSuggestionDictDO;
    }
    /**
     * 建议类型下移
     * @param
     * @return
     * @throws Exception
     */
    public BaseSuggestionDictDO downSuggestion(String id) throws Exception{
        BaseSuggestionDictDO baseSuggestionDictDO = baseSuggestionDictDao.findOne(id);
        int minSort = baseSuggestionDictDao.getMinSort();
        int sort = 0;
        if(null!=baseSuggestionDictDO){
            sort = baseSuggestionDictDO.getIsSort();
        }
        if (minSort==sort){
            throw new Exception("不能下移");
        }
        String sql = " select id AS \"id\" from base_suggestions_dict where is_sort < "+sort+" order by is_sort desc ";
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,1,1);
        BaseSuggestionDictDO downPrevious = new BaseSuggestionDictDO();
        if (list.size()>0){
            String prviousId = list.get(0).get("id").toString();
            downPrevious =baseSuggestionDictDao.findOne(prviousId);
        }
        //交换sort值
        baseSuggestionDictDO.setIsSort(downPrevious.getIsSort());
        downPrevious.setIsSort(sort);
        baseSuggestionDictDao.save(baseSuggestionDictDO);
        baseSuggestionDictDao.save(downPrevious);
        return baseSuggestionDictDO;
    }


    /**
     * 建议类型新增
     * @param
     * @return
     * @throws Exception
     */
    public BaseSuggestionDictDO createSuggestionType(BaseSuggestionDictDO baseSuggestionDictDO){
        String sql = "select max(is_sort) as \"isSort\" from base_suggestions_dict";
        List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
        Map<String,Object> map = list.get(0);
        int sort = 0;
        if (null!=map.get("isSort")){
            if(null!=map){
                sort = Integer.valueOf(map.get("isSort").toString());
            }
        }
        baseSuggestionDictDO.setIsSort(sort+1);
        baseSuggestionDictDO.setIsDel(1);
        baseSuggestionDictDO.setUpdateTime(new Date());
        baseSuggestionDictDO.setCreateTime(new Date());
        BaseSuggestionDictDO result =  baseSuggestionDictDao.save(baseSuggestionDictDO);
        return result;
    }
    /**
     * 更新
     * @param
     * @return
     * @throws Exception
     */
    public BaseSuggestionDictDO updateSuggest(String id,String suggestName,String suggestDes,String updateUser,String updateUserName){
        BaseSuggestionDictDO baseSuggestionDictDO = baseSuggestionDictDao.findOne(id);
        if (null == baseSuggestionDictDO){
            return null;
        }
        if (!StringUtil.isEmpty(suggestName)){
            baseSuggestionDictDO.setSuggestName(suggestName);
            baseSuggestionDictDO.setUpdateTime(new Date());
        }
        if (!StringUtil.isEmpty(suggestDes)){
            baseSuggestionDictDO.setSuggestDescribe(suggestDes);
        }
        if (!StringUtil.isEmpty(updateUser)){
            baseSuggestionDictDO.setUpdateUser(updateUser);
        }
        if (!StringUtil.isEmpty(updateUserName)){
            baseSuggestionDictDO.setUpdateUser(updateUserName);
        }
        baseSuggestionDictDao.save(baseSuggestionDictDO);
        return baseSuggestionDictDO;
    }
    /**
     * 删除
     * @param
     * @return
     * @throws Exception
     */
    public Boolean deleteSuggest(String id){
        BaseSuggestionDictDO baseSuggestionDictDO = baseSuggestionDictDao.findOne(id);
        if (null == baseSuggestionDictDO){
            return false;
        }
        baseSuggestionDictDao.delete(id);
        return true;
    }
    /*
    *   根据建议名称 或者描述查询
    *
     */
    public MixEnvelop findByNameAndDes(String name, String des, Integer page, Integer pageSize){
        String sql = "select t.id as\"id\",t.suggest_name as\"suggestName\",t.suggest_describe as\"suggestDescribe\"," +
                "t.is_del as\"isDel\",t.is_sort as\"isSort\",t.create_time as\"createTime\",t.update_time as \"updateTime\"" +
                " from base_suggestions_dict t where 1=1 and t.is_del = 1";
        if(StringUtils.isNotEmpty(name)){
            sql+=" and t.suggest_name like '%"+name+"%'";
        }
        if(StringUtils.isNotEmpty(des)){
            sql+=" and t.suggest_describe like '%"+des+"%'";
        }
        sql+= " order by t.is_sort asc";
        List<Map<String,Object>> list= hibenateUtils.createSQLQuery(sql,page,pageSize);
        for (Map<String,Object> map:list){
            String createTime= map.get("createTime").toString();
            String updateTime = map.get("updateTime").toString();
            map.put("createTime", DateUtil.dateToStrLong(DateUtil.strToDateLong(createTime)));
            map.put("updateTime",DateUtil.dateToStrLong(DateUtil.strToDateLong(updateTime)));
        }
        MixEnvelop mixEnvelop = new MixEnvelop();
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setTotalCount(list.size());
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setCurrPage(page);
        return mixEnvelop;
    }
}
