package com.bweb.notify;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.entity.BsMer;
import com.hfkj.entity.BsMerContractRecord;
import com.hfkj.entity.BsMerContractRecordMsg;
import com.hfkj.service.BsMerContractRecordMsgService;
import com.hfkj.service.BsMerContractRecordService;
import com.hfkj.service.BsMerService;
import com.hfkj.sysenum.MerStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
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.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;


/**
 * 拉卡拉通知
 * @author hurui
 */
@Controller
@Api(value = "拉卡拉通知")
@RequestMapping(value = "/laKaLaNotify")
public class LaKaLaController {

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

    @Resource
    private BsMerContractRecordMsgService merContractRecordMsgService;

    @Resource
    private BsMerContractRecordService merContractRecordService;

    @Resource
    private BsMerService merService;

    @RequestMapping(value="/merContract",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "商户进件通知")
    public void merContract(@RequestBody String reqBody, HttpServletResponse response){
        try {
            log.info("商户进件通知", reqBody);
            System.out.println(reqBody);

            JSONObject bodyObject = JSONObject.parseObject(reqBody);
            JSONObject data = bodyObject.getJSONObject("data");

            // 订单号
            String orderNo = data.getString("orderNo");

            // 进件状态
            // 未提交：NO_COMMIT
            // 已提交 COMMIT
            // 提交失败：COMMIT_FAIL
            // 转人工审核：MANUAL_AUDIT
            // 审核中：REVIEW_ING
            // 审核通过：WAIT_FOR_CONTACT
            // 审核驳回：INNER_CHECK_REJECTED
            String contractStatus = data.getString("contractStatus");

            // 响应记录
            BsMerContractRecordMsg record = new BsMerContractRecordMsg();
            record.setType(2);
            record.setOrderNo(orderNo);
            record.setResponseCode(bodyObject.getString("code"));
            record.setResponseMsg(bodyObject.getString("message"));
            record.setResponseContent(reqBody);
            merContractRecordMsgService.insertRecordMsg(record);

            if (contractStatus.equals("WAIT_FOR_CONTACT")) {
                // 查询进件流水
                BsMerContractRecord contractRecord = merContractRecordService.getRecordByOrderNo(orderNo);
                if (contractRecord != null) {
                    merService.laKaLaMerContractNotify(contractRecord.getMerId(), MerStatusEnum.status1, data);
                }
            }

            if (contractStatus.equals("INNER_CHECK_REJECTED")) {
                // 查询进件流水
                BsMerContractRecord contractRecord = merContractRecordService.getRecordByOrderNo(orderNo);
                if (contractRecord != null) {
                    merService.laKaLaMerContractNotify(contractRecord.getMerId(), MerStatusEnum.status4, data);
                }
            }

            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
            JSONObject result = new JSONObject();
            result.put("code", "SUCCESS");
            result.put("message", "成功");
            out.write(result.toJSONString().getBytes());
            out.flush();
            out.close();
        } catch (Exception e) {
            log.error("login error!",e);
        }

    }
}
