package com.cweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsUserReceipt;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.user.BsUserReceiptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: BsUserReceiptController
 * @author: HuRui
 * @date: 2024/7/24
 **/
@Controller
@RequestMapping(value = "/userReceipt")
@Api(value = "用户收据")
public class BsUserReceiptController {

    private static Logger log = LoggerFactory.getLogger(BsUserReceiptController.class);
    @Resource
    private UserCenter userCenter;
    @Resource
    private BsUserReceiptService userReceiptService;

    @RequestMapping(value = "/editReceipt", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑收据")
    public ResponseData editReceipt(@RequestBody BsUserReceipt body) {
        try {
            UserSessionObject sessionObject = userCenter.getSessionModel(UserSessionObject.class);
            if (sessionObject == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }
            if (body == null
                || StringUtils.isBlank(body.getCompanyName())
                || StringUtils.isBlank(body.getTaxNo())
                || StringUtils.isBlank(body.getContactsPhone())
                || StringUtils.isBlank(body.getEmail())
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            BsUserReceipt receipt = null;
            if (body.getId() != null) {
                // 查询数据
                receipt = userReceiptService.getDetail(body.getId());
                if (receipt == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的数据");
                }
            } else {
                receipt = new BsUserReceipt();
                receipt.setUserId(sessionObject.getUser().getId());
                receipt.setStatus(1);
            }
            receipt.setType(1);
            receipt.setCompanyName(body.getCompanyName());
            receipt.setTaxNo(body.getTaxNo());
            receipt.setContactsPhone(body.getContactsPhone());
            receipt.setEmail(body.getEmail());
            userReceiptService.editData(receipt);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/queryDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询详情")
    public ResponseData queryDetail(@RequestParam(name = "id", required = true) Long id) {
        try {

            return ResponseMsgUtil.success(userReceiptService.getDetail(id));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/queryUserReceiptList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询用户票据")
    public ResponseData queryUserReceiptList(@RequestParam(name = "status", required = false) String status,
                                             @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                             @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            UserSessionObject sessionObject = userCenter.getSessionModel(UserSessionObject.class);
            if (sessionObject == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }

            Map<String,Object> param = new HashMap<>();
            param.put("userId", sessionObject.getUser().getId());
            param.put("status", status);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(userReceiptService.getList(param)));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
