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

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.outpatient.interactive.ComplaintsDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.outpatient.interactive.complaints.request.ComplaintsInquiry;
import thyyxxk.webserver.entity.outpatient.interactive.complaints.response.ComplaintsAndSuggestions;
import thyyxxk.webserver.service.externalhttp.WxServer;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ComplaintsService {
    private final ComplaintsDao dao;
    private final WxServer wxServer;
    private final UserCache userCache;


    public ResultVo<List<ComplaintsAndSuggestions>> selectAllComplaints(ComplaintsInquiry inquiry) {
        String statement = "";
        if (inquiry.getReplied() == 0) {
            statement = "and replied=0";
        } else if (inquiry.getReplied() == 1) {
            statement = "and replied=1";
        }
        inquiry.setExtraStatement(statement);
        List<ComplaintsAndSuggestions> list = dao.selectAllComplaints(inquiry);
        if (list.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        for (ComplaintsAndSuggestions item : list) {
            item.setHandleStaff(userCache.getEmployeeName(item.getHandleStaff()));
        }
        return ResultVoUtil.success(list);
    }

    public ResultVo<String> sendResponse(ComplaintsAndSuggestions complaint) {
        if (StringUtil.isBlank(complaint.getResponse())) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "回复内容不能为空！");
        }
        String msgContent = "{\"touser\":\"" + complaint.getOpenid() + "\",\"data\":" +
                "{\"keyword1\":{\"color\":\"#173177\",\"value\":\"" + complaint.getContent() + "\"}," +
                "\"keyword2\":{\"color\":\"#173177\",\"value\":\"" + DateUtil.formatDatetime(complaint.getCreateDatetime()) + "\"}," +
                "\"keyword3\":{\"color\":\"#173177\",\"value\":\"" + complaint.getResponse() + "\"}," +
                "\"remark\":{\"color\":\"#FF0000\",\"value\":\"非常感谢您宝贵的意见，祝您生活愉快，谢谢！\"}," +
                "\"first\":{\"color\":\"#FF0000\",\"value\":\"尊敬的用户您好，您的投诉建议已受理！\"}}," +
                "\"template_id\":\"P08Fcfy5RyLR3fvlfZMFxeCbLTGFTPrvQRwCDNjDKCo\"," +
                "\"url\":\"\"}";
        String result = wxServer.pushMessage2(JSONObject.parseObject(msgContent));
        if (result.equals("SUCCESS")) {
            complaint.setHandleStaff(TokenUtil.getInstance().getTokenUserId());
            dao.updateResponse(complaint);
            return ResultVoUtil.success("回复成功。");
        }
        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, result);
    }
}
