package thyyxxk.webserver.service.outpatient.interactive.comments;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.outpatient.interactive.CommentsDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.outpatient.interactive.comments.request.CommentInquiry;
import thyyxxk.webserver.entity.outpatient.interactive.comments.response.CommentsResponse;
import thyyxxk.webserver.entity.outpatient.interactive.comments.response.WechatPatientComment;
import thyyxxk.webserver.service.hutoolcache.DeptCache;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.TokenUtil;

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

@Service
public class CommentsService {
    private final CommentsDao dao;
    private final DeptCache deptCache;

    @Autowired
    public CommentsService(CommentsDao dao, DeptCache deptCache) {
        this.dao = dao;
        this.deptCache = deptCache;
    }

    public ResultVo<CommentsResponse> getComments(CommentInquiry inquiry) {
        QueryWrapper<?> wrapper = new QueryWrapper<>();
        if (null != inquiry.getDeleted()) {
            wrapper.eq("deleted", inquiry.getDeleted());
        }
        if (null != inquiry.getCommentLevel()) {
            wrapper.eq("comment_level", inquiry.getCommentLevel());
        }
        if (StringUtil.notBlank(inquiry.getDoctorName())) {
            wrapper.eq("doctor_name", inquiry.getDoctorName());
        }
        if (StringUtil.notBlank(inquiry.getStartTime()) && StringUtil.notBlank(inquiry.getEndTime())) {
            wrapper.ge("comment_time", inquiry.getStartTime())
                    .le("comment_time", inquiry.getEndTime());
        }
        if (inquiry.getOnlyValidComments()) {
            wrapper.apply("comment_content!='未填写评价内容。' and isnull(comment_content,'')!=''");
        }
        if (inquiry.getIsForExport()) {
            inquiry.setPageNum(1);
            inquiry.setPageSize(Integer.MAX_VALUE);
        }
        List<WechatPatientComment> list = dao.selectComments(wrapper, inquiry.getPageNum(), inquiry.getPageSize());
        if (list.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        list.forEach(item -> item.setDepartment(deptCache.getDeptName(item.getDepartment())));
        CommentsResponse response = new CommentsResponse();
        response.setTotalSize(dao.selectValidCount(wrapper));
        response.setList(list);
        return ResultVoUtil.success(response);
    }

    public ResultVo<String> updateCommentStatus(int id, int deleted) {
        String staff = null;
        Date date = null;
        if (deleted == 1) {
            staff = TokenUtil.getInstance().getTokenUserId();
            date = new Date();
        }
        int count = dao.updateCommentStatus(id, deleted, staff, date);
        if (count == 1) {
            return ResultVoUtil.success("操作成功。");
        }
        return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
    }
}
