package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighApprove;
import com.hai.enum_type.ApproveType;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighApproveService;
import com.hai.service.HighCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName HighApproveController
 * @Description: 审批操作
 * @Author 胡锐
 * @Date 2021/3/16
 **/
@Controller
@RequestMapping(value = "/audit")
@Api(value = "审核接口")
public class HighApproveController {

    private static Logger log = LoggerFactory.getLogger(HighApproveController.class);

    @Autowired
    private UserCenter userCenter;

    @Resource
    private HighApproveService highApproveService;

    @Resource
    private HighCouponService highCouponService;

    @RequestMapping(value="/approveProcessed",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "审批")
    public ResponseData approveProcessed(@RequestBody String reqBody, HttpServletRequest request) {
        try {
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            JSONObject jsonObject = JSONObject.parseObject(reqBody);
            Long approveId = jsonObject.getLong("approveId");
            Integer status = jsonObject.getInteger("status");
            String remarks = jsonObject.getString("remarks");
            if(approveId == null || status == null) {
                log.error("HighCouponController -> approveProcessed() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询审批记录
            HighApprove approve = highApproveService.getApproveById(approveId);
            if (approve == null) {
                log.error("HighCouponController -> approveProcessed() error!","未找到审批记录");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_APPROVE, "");
            }
            if (approve.getStatus() != 1) {
                log.error("HighCouponController -> approveProcessed() error!","审批已处理");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.APPROVE_PROCESSED, "");
            }

            approve.setRemarks(remarks);
            approve.setStatus(status);
            approve.setUpdateTime(new Date());
            approve.setApproveOperatorId(userInfoModel.getSecUser().getId());
            approve.setApproveOperatorName(userInfoModel.getSecUser().getUserName());
            highApproveService.approveProcessed(approve);

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

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

    @RequestMapping(value="/getApproveDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询审批详情")
    public ResponseData getApproveDetail(@RequestParam(name = "approveId", required = true) Long approveId) {
        try {
            // 查询审批记录
            HighApprove approve = highApproveService.getApproveById(approveId);
            if (approve == null) {
                log.error("HighCouponController -> getApproveDetail() error!","未找到审批记录");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_APPROVE, "");
            }
            Map<String, Object> map = new HashMap<>();
            map.put("approve", approve);

            if (approve.getObjectType().equals(ApproveType.UP_SHELF_APPROVE.getType())) {
                map.put("object", highCouponService.getCouponById(approve.getObjectId()));
            }

            return ResponseMsgUtil.success(map);

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

    @RequestMapping(value="/getApproveList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询审批列表")
    public ResponseData getApproveList(@RequestParam(name = "approveSerialNo", required = false) String approveSerialNo,
                                       @RequestParam(name = "objectType", required = false) Integer objectType,
                                       @RequestParam(name = "objectId", required = false) Long objectId,
                                       @RequestParam(name = "objectName", required = false) String objectName,
                                       @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                       @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

            Map<String, Object> map = new HashMap<>();
            map.put("approveSerialNo", approveSerialNo);
            map.put("objectType", objectType);
            map.put("objectId", objectId);
            map.put("objectName", objectName);

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(highApproveService.getApproveList(map)));

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