package thyyxxk.webserver.controller.outpatient.importdrug;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.outpatient.DrugPurchaseDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.outpatient.importdrug.DrugPurchase;
import thyyxxk.webserver.entity.outpatient.importdrug.PurchaseInquiry;
import thyyxxk.webserver.service.externalhttp.WxServer;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/drugPurchase")
public class DrugPurchaseController {
    private final DrugPurchaseDao dao;
    private final WxServer wxService;

    @Autowired
    public DrugPurchaseController(DrugPurchaseDao dao, WxServer wxService) {
        this.dao = dao;
        this.wxService = wxService;
    }

    @PostMapping("/findByCondition")
    public ResultVo<List<DrugPurchase>> findByCondition(@RequestBody PurchaseInquiry inquiry) {
        QueryWrapper<DrugPurchase> wrapper = new QueryWrapper<>();
        wrapper.ge("create_time", inquiry.getStart());
        wrapper.le("create_time", inquiry.getEnd());
        if (null != inquiry.getDealFlag()) {
            wrapper.eq("deal_flag", inquiry.getDealFlag());
        }
        if (null != inquiry.getPrepayState()) {
            wrapper.eq("prepay_state", inquiry.getPrepayState());
        }
        wrapper.orderByDesc("create_time");
        return ResultVoUtil.success(dao.selectList(wrapper));
    }

    @PostMapping("/dealPurchase")
    public ResultVo<String> dealPurchase(@RequestBody DrugPurchase purchase) {
        purchase.setDealFlag(1);
        purchase.setDealStaff(TokenUtil.getInstance().getTokenUserId());
        purchase.setDealTime(new Date());
        dao.updateById(purchase);
        return ResultVoUtil.success("处理成功。");
    }

    @PostMapping("/setPrepayAmt")
    public ResultVo<String> setPrepayAmt(@RequestBody DrugPurchase purchase) {
        if (null == purchase.getPrepayAmt()) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "订金金额不能为空。");
        }
        if (purchase.getPrepayAmt().compareTo(BigDecimal.ZERO) < 0) {
            return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "订金金额不能小于0");
        }
        dao.updateById(purchase);
        String msg = pushWxMessage(purchase);
        if (!msg.equals("SUCCESS")) {
            return ResultVoUtil.success("设置成功，但" + msg);
        }
        return ResultVoUtil.success("设置成功。");
    }

    private String pushWxMessage(DrugPurchase purchase) {
        String msgContent = "{\"touser\":\"" + purchase.getOpenid() + "\",\"data\":" +
                "{\"thing4\":{\"value\":\"药品申购订金\"}," +
                "\"amount3\":{\"value\":\"" + purchase.getPrepayAmt() + "\"}," +
                "}," +
                "\"template_id\":\"UaJqBNVEpyOGcHhKvIa848DVj1xLDYKAV1aUsQRMLB0\"," +
                "\"url\":\"https://staticweb.hnthyy.cn/wxserver/redirect/pathEntry?path=historyPurchase\"}";
        return wxService.pushMessage2(JSONObject.parseObject(msgContent));
    }
}
