package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.JsonUtil;
import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.thmz.Receipt;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.service.thmz.ReceiptService;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class ReceiptController {
    @Autowired
    private ReceiptService receiptService;

    /**
     * 保存发票信息
     *
     * @param receipt
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveReceipt", method = {RequestMethod.POST})
    public Map<String, Object> saveReceipt(@RequestBody Receipt receipt, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        if (receipt == null) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存发票信息失败，参数为空");
            return resultMap;
        }
        if (StringUtils.isBlank(receipt.getReceiptBegin())&& StringUtils.isBlank(receipt.getZyReceiptBegin())) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存发票信息失败，门诊发票起始编号和住院发票起始编号不能同时为空");
            return resultMap;
        }
        if (StringUtils.isNotBlank(receipt.getReceiptBegin()) && receipt.getReceiptBegin().length() != 8) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存发票信息失败，门诊发票起始编号位数有误，请检查");
            return resultMap;
        }
        if (StringUtils.isNotBlank(receipt.getZyReceiptBegin())&& receipt.getZyReceiptBegin().length() != 8) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存发票信息失败，住院发票起始编号位数有误，请检查");
            return resultMap;
        }
        if (StringUtils.isBlank(receipt.getReceiptEnd()) && StringUtils.isBlank(receipt.getZyReceiptEnd())) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存发票信息失败，门诊发票结束编号和住院发票结束编号不能同时为空");
            return resultMap;
        }
        if (StringUtils.isNotBlank(receipt.getReceiptEnd()) && receipt.getReceiptEnd().length() != 8) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存发票信息失败，门诊发票结束编号位数有误，请检查");
            return resultMap;
        }
        if (StringUtils.isNotBlank(receipt.getZyReceiptEnd()) && receipt.getZyReceiptEnd().length() != 8) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存发票信息失败，住院发票结束编号位数有误，请检查");
            return resultMap;
        }
        String token = TokenUtil.getToken(httpServletRequest);
        if (StringUtils.isBlank(token)) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存发票信息失败,用户Token不存在");
            return resultMap;
        }
        DecodedJWT decodedJWT = TokenUtil.parseJWT(token);
        User tokenUser = (User) JsonUtil.jsontoObject(decodedJWT.getSubject(), User.class);
        try {
            Receipt receiptDb = receiptService.queryByUserIdCode(tokenUser.getUserIdCode());
            if (receiptDb != null) {
//                if(receiptDb.getReceiptCurrent()!=null &&!receiptDb.getReceiptBegin().equals(receiptDb.getReceiptCurrent())){
//                    resultMap.put("code", -1);
//                    resultMap.put("message", "您有正在使用中的发票,请在发票使用完后再维护！");
//                    resultMap.put("data",receipt);
//                    return resultMap;
//                }
                receipt.setId(receiptDb.getId());
            }
            receipt.setUserIdCode(tokenUser.getUserIdCode());
            receipt.setReceiptCurrent(receipt.getReceiptBegin());
            receipt.setZyReceiptCurrent(receipt.getZyReceiptBegin());
            int num = receiptService.saveReceipt(receipt);
            if (num == 1) {
                resultMap.put("code", 0);
                resultMap.put("message", "保存发票信息成功");
                return resultMap;
            }
            resultMap.put("code", -1);
            resultMap.put("message", "保存发票信息失败");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "保存发票信息失败,错误信息：【" + e.getMessage() + "】");
            log.error("保存发票信息失败,错误信息：[]", e.getMessage());
            return resultMap;
        }
    }


    /**
     * 查询用户的发票信息
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getReceiptByCurrentUser", method = {RequestMethod.GET})
    public Map<String, Object> getReceiptByCurrentUser(HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        String token = TokenUtil.getToken(httpServletRequest);
        if (StringUtils.isBlank(token)) {
            resultMap.put("code", -1);
            resultMap.put("message", "查询用户的发票信息失败,用户Token不存在");
            return resultMap;
        }
        DecodedJWT decodedJWT = TokenUtil.parseJWT(token);
        User tokenUser = (User) JsonUtil.jsontoObject(decodedJWT.getSubject(), User.class);
        try {
            Receipt receipt = receiptService.queryByUserIdCode(tokenUser.getUserIdCode());
            if (receipt != null) {
                resultMap.put("code", 0);
                resultMap.put("message", "查询用户的发票信息成功");
                resultMap.put("data", receipt);
                return resultMap;
            }
            resultMap.put("code", -2);
            resultMap.put("message", "查询用户的发票信息失败");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询用户的发票信息失败,错误信息：【" + e.getMessage() + "】");
            log.error("查询用户的发票信息失败,错误信息：[]", e.getMessage());
            return resultMap;
        }
    }
}
