package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
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.utils.ResponseMsgUtil;
import com.hfkj.entity.BsGasOrderAudit;
import com.hfkj.model.ResponseData;
import com.hfkj.service.gas.BsGasOrderAuditService;
import com.hfkj.service.gas.BsGasOrderService;
import com.hfkj.sysenum.gas.GasOrderAuditStatusEnum;
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;

@Controller
@RequestMapping(value = "/gasOrderAudit")
@Api(value = "加油站价格")
public class BsGasOrderAuditController {

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

    @Resource
    private BsGasOrderAuditService gasOrderAuditService;
    @Resource
    private BsGasOrderService gasOrderService;

    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "审核")
    public ResponseData audit(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("auditNo"))
                    || body.getBoolean("result") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            if (body.getBoolean("result") == false && StringUtils.isBlank(body.getString("rejectRemark"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请填写驳回备注");
            }
            // 审核数据
            BsGasOrderAudit audit = gasOrderAuditService.getDetail(body.getString("auditNo"));
            if (audit == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到审核数据");
            }
            if (!audit.getStatus().equals(GasOrderAuditStatusEnum.type1.getCode())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "审核不处于待审核状态");
            }
            if (body.getBoolean("result") == true) {
                gasOrderAuditService.approval(audit.getAuditNo(),body.getString("rejectRemark"));
            } else {
                gasOrderAuditService.reject(audit.getAuditNo(),body.getString("rejectRemark"));
            }
            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value = "/queryDetail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询详情")
    public ResponseData queryDetail(@RequestParam(name = "auditNo", required = true) String auditNo) {
        try {
            // 审核数据
            BsGasOrderAudit audit = gasOrderAuditService.getDetail(auditNo);
            if (audit == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到审核数据");
            }
            Map<String,Object> map = new HashMap<>();
            map.put("audit", audit);
            map.put("gasOrder", gasOrderService.getDetailByOrderNo(audit.getOrderNo()));
            return ResponseMsgUtil.success(map);

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

    @RequestMapping(value = "/queryList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData queryList(@RequestParam(name = "auditNo", required = false) String auditNo,
                                  @RequestParam(name = "channelType", required = false) Integer channelType,
                                  @RequestParam(name = "channelOrderNo", required = false) String channelOrderNo,
                                  @RequestParam(name = "orderNo", required = false) String orderNo,
                                  @RequestParam(name = "agentId", required = false) Long agentId,
                                  @RequestParam(name = "merNo", required = false) String merNo,
                                  @RequestParam(name = "merName", required = false) String merName,
                                  @RequestParam(name = "status", required = false) Integer status,
                                  @RequestParam(name = "createTimeS", required = false) Long createTimeS,
                                  @RequestParam(name = "createTimeE", required = false) Long createTimeE,
                                  @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                  @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("auditNo", auditNo);
            param.put("channelType", channelType);
            param.put("channelOrderNo", channelOrderNo);
            param.put("orderNo", orderNo);
            param.put("agentId", agentId);
            param.put("merNo", merNo);
            param.put("merName", merName);
            param.put("status", status);
            param.put("createTimeS", createTimeS);
            param.put("createTimeE", createTimeE);

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

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