package com.ssm.controller;

import com.ssm.core.bean.GenericResponse;
import com.ssm.core.bean.PageResponse;
import com.ssm.manage.entity.StoreAllinpayInfo;
import com.ssm.manage.query.StoreAllinpayInfoRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmStoreAllinpayinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.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.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(value = "门店管理通联账户信息controller", tags = {"门店管理通联账户信息相关接口"})
@RestController
@RequestMapping("/demay/ssm/storeAllipay")
@Slf4j
public class SsmStoreAllipayController {

    @Autowired
    private SsmStoreAllinpayinfoService ssmStoreAllinpayinfoService;

    @ApiOperation(value = "查询门店通联账户信息 美发版-查询门店通联账户信息", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/list")
    public GenericResponse selectPage(@RequestBody StoreAllinpayInfoRequest storeAllinpayInfoRequest) {
        GenericResponse genericResponse = new GenericResponse();
        List<StoreAllinpayInfo> list = new ArrayList<>();
        try {
            list = ssmStoreAllinpayinfoService.selectList(storeAllinpayInfoRequest);
        } catch (Exception e) {
            log.error("SsmStoreAllipayController.list", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setRows(list);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "添加门店通联账户信息  美发版-添加门店通联账户信息", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody StoreAllinpayInfoRequest storeAllinpayInfoRequest) {
        GenericResponse genericResponse = new GenericResponse<>();
        if (StringUtils.isBlank(storeAllinpayInfoRequest.getAppId())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"应用ID");
            return genericResponse;
        }
        if (StringUtils.isBlank(storeAllinpayInfoRequest.getCusId())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "商户号");
            return genericResponse;
        }
//        if (StringUtils.isBlank(storeAllinpayInfoRequest.getOrgId())) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "集团商户号");
//            return genericResponse;
//        }
//        if (StringUtils.isBlank(storeAllinpayInfoRequest.getMd5Appkey())) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "md5格式签名");
//            return genericResponse;
//        }
        if (StringUtils.isBlank(storeAllinpayInfoRequest.getRsaTlpubkey())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "rsa格式签名公钥");
            return genericResponse;
        }
        if (StringUtils.isBlank(storeAllinpayInfoRequest.getRsaCusprikey())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "rsa格式签名私钥");
            return genericResponse;
        }
//        if (StringUtils.isBlank(storeAllinpayInfoRequest.getSm2Privatekey())) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "sm2格式签名私钥");
//            return genericResponse;
//        }
//        if (StringUtils.isBlank(storeAllinpayInfoRequest.getSm2Publickey())) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "sm2格式签名公钥");
//            return genericResponse;
//        }
        int i = 0;
        try {
            i = ssmStoreAllinpayinfoService.add(storeAllinpayInfoRequest);
        } catch (Exception e) {
            log.error("SsmStoreAllipayController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "修改门店通联账户信息  美发版-修改门店通联账户信息", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody StoreAllinpayInfoRequest storeAllinpayInfoRequest) {
        GenericResponse genericResponse = new GenericResponse<>();
        int i = 0;
        try {
            i = ssmStoreAllinpayinfoService.update(storeAllinpayInfoRequest);
        } catch (Exception e) {
            log.error("SsmStoreAllipayController.update", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

}
