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


import com.yihu.jw.entity.hospital.suggest.BaseSuggestionDO;
import com.yihu.jw.entity.hospital.suggest.BaseSuggestionDictDO;
import com.yihu.jw.hospital.suggest.dao.BaseSuggestionDao;
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.hibernate.HibenateUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BaseSuggestionService extends BaseJpaService<BaseSuggestionDO, BaseSuggestionDao> {
    @Autowired
    private HibenateUtils hibenateUtils;
    @Autowired
    private BaseSuggestionDao baseSuggestionDao;
    @Autowired
    private BaseSuggestionDictDao baseSuggestionDictDao;
    /**
     * 新增反馈建议
     * @param
     * @return
     * @throws Exception
     */
    public BaseSuggestionDO createSuggestion(BaseSuggestionDO baseSuggestionDO){
        baseSuggestionDO.setUpdateTime(new Date());
        baseSuggestionDO.setCreateTime(new Date());
        baseSuggestionDO.setIsDel(1);
        baseSuggestionDO.setSuggestStatus(0);
        BaseSuggestionDO result =  baseSuggestionDao.save(baseSuggestionDO);
        return result;
    }
    /**
     * 建议查看
     * @param
     * @return
     * @throws Exception
     */
    public MixEnvelop findSuggestionByType(String type,String senderId, Integer page, Integer pageSize){
        String sql = "select a.id as\"id\",a.suggest_id as\"suggestId\",a.suggest_content as\"suggestContent\",a.suggest_img_url as\"suggestImgUrl\"," +
                "a.suggest_user_id as\"suggestUserId\",a.user_client as \"userClient\",a.user_type as \"userType\"," +
                "a.suggest_user_name as\"suggestUserName\",a.suggest_user_phone as\"suggestUserPhone\",a.create_time as\"createTime\"," +
                "a.update_time as\"updateTime\",a.suggest_status as \"suggestStatus\"," +
                "b.suggest_name as\"suggestName\",b.suggest_describe as\"suggestSescribe\" from base_suggestion a ," +
                "base_suggestions_dict b where a.suggest_id = b.id and a.is_del = 1" ;
        if(StringUtils.isNoneBlank(type)){
            sql+=" and a.suggest_id = '"+type+"'";
        }if (StringUtils.isNoneBlank(senderId)){
            sql+=" and a.suggest_user_id = '"+senderId+"'";
        }
        sql+=" order by a.create_time desc";
        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.setCurrPage(page);
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setTotalCount(list.size());
        mixEnvelop.setDetailModelList(list);
        return mixEnvelop;
    }
    //回复反馈
    public void reponseSuggestion(String id,String responseContent,String userId,String userName){
        BaseSuggestionDO baseSuggestionDO = baseSuggestionDao.findOne(id);
        if (null!=baseSuggestionDO){
            baseSuggestionDO.setManagerRes(responseContent);
            baseSuggestionDO.setResUser(userId);
            baseSuggestionDO.setResUserName(userName);
            baseSuggestionDO.setSuggestStatus(1);
        }
        baseSuggestionDao.save(baseSuggestionDO);
    }
    //查看单条
    public BaseSuggestionDO findSuggestionById(String id){
        BaseSuggestionDO baseSuggestionDO = baseSuggestionDao.findOne(id);
        BaseSuggestionDictDO baseSuggestionDictDO = baseSuggestionDictDao.findOne(baseSuggestionDO.getSuggestId());
        baseSuggestionDO.setSuggestName(baseSuggestionDictDO.getSuggestName());
        return baseSuggestionDO;

    }


}
