package com.bweb.controller.mer;

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.security.SessionObject;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMer;
import com.hfkj.entity.BsMerPayConfig;
import com.hfkj.entity.GoodsMsg;
import com.hfkj.entity.GoodsType;
import com.hfkj.mer.BsMerPayConfigService;
import com.hfkj.model.BsMerModel;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.BsMerService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import com.hfkj.sysenum.mer.PayPlatformEnum;
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 javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: BsMerController
 * @author: HuRui
 * @date: 2024/4/24
 **/
@Controller
@RequestMapping(value="/mer")
@Api(value="商户业务")
public class BsMerController {

    Logger log = LoggerFactory.getLogger(BsMerController.class);
    @Resource
    private UserCenter userCenter;
    @Resource
    private BsMerService merService;

    @Resource
    private BsMerPayConfigService bsMerPayConfigService;

    @RequestMapping(value="/create",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建商户")
    public ResponseData create(@RequestBody BsMerModel body) {
        try {
            if (body == null
                || StringUtils.isBlank(body.getLoginName())
                || body.getRoleId() == null
                || StringUtils.isBlank(body.getMerName())
                || StringUtils.isBlank(body.getDirectorName())
                || StringUtils.isBlank(body.getDirectorTel())
                || StringUtils.isBlank(body.getAddress())
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            SecUserSessionObject sessionModel = userCenter.getSessionModel(SecUserSessionObject.class);
            if (sessionModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }

            body.setOpUserId(sessionModel.getAccount().getId());
            body.setOpUserName(sessionModel.getAccount().getUserName());
            merService.create(body);

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

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

    @RequestMapping(value="/update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改商户")
    public ResponseData update(@RequestBody BsMerModel body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getMerNo())
                    || body.getRoleId() == null
                    || StringUtils.isBlank(body.getMerName())
                    || StringUtils.isBlank(body.getDirectorName())
                    || StringUtils.isBlank(body.getDirectorTel())
                    || StringUtils.isBlank(body.getAddress())
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询商户
            BsMerModel mer = merService.getDetail(body.getMerNo());
            if (mer == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "商户不存在");
            }

            mer.setRoleId(body.getRoleId());
            mer.setMerLogo(body.getMerLogo());
            mer.setMerName(body.getMerName());
            mer.setDirectorName(body.getDirectorName());
            mer.setDirectorTel(body.getDirectorTel());
            mer.setCustomerServiceTel(body.getCustomerServiceTel());
            mer.setAddress(body.getAddress());
            merService.update(mer);

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

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

    @RequestMapping(value="/resetPwd",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "重置密码")
    public ResponseData resetPwd(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("merNo"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            merService.resetPwd(body.getString("merNo"));

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

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


    @RequestMapping(value="/queryMer",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户")
    public ResponseData queryMer(@RequestParam(value = "merNo" , required = true) String merNo) {
        try {

            return ResponseMsgUtil.success(merService.getDetail(merNo));

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

    @RequestMapping(value="/queryList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户列表")
    public ResponseData queryList(@RequestParam(value = "merNo" , required = false) String merNo,
                                  @RequestParam(value = "merName" , required = false) String merName,
                                  @RequestParam(value = "status" , required = false) Integer status,
                                  @RequestParam(value = "opUserName" , required = false) String opUserName,
                                  @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                  @RequestParam(value = "pageSize" , required = true) Integer pageSize) {
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("merNo", merNo);
            param.put("merName", merName);
            param.put("opUserName", opUserName);
            param.put("status", status);

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

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




    @RequestMapping(value="/editPayConfig",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建商户支付内容")
    public ResponseData editPayConfig(@RequestBody BsMerPayConfig body, HttpServletRequest request) {

        try {


            if (body == null
                    || body.getMerId() == null
                    || body.getPayPlatform() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            if (body.getPayPlatform() == PayPlatformEnum.type1.getCode()) {
                if ( body.getPayMerNo() == null
                        || body.getPayMerKey() == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            } else if (body.getPayPlatform() == PayPlatformEnum.type2.getCode()) {
                if ( body.getHltMerNo() == null
                        || body.getHltInstCode() == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            }

            Map<String, Object> param = new HashMap<>();
            param.put("merId", body.getMerId());
            param.put("payPlatform", body.getPayPlatform());

            BsMerPayConfig merPayConfig =  bsMerPayConfigService.queryDetailByMap(param);

            if (merPayConfig == null) {
                merPayConfig = new BsMerPayConfig();
                merPayConfig.setCreateTime(new Date());
            }

            BsMer bsMer = merService.getDetailById(body.getMerId());



            merPayConfig.setUpdateTime(new Date());
            merPayConfig.setMerId(body.getMerId());
            merPayConfig.setMerNo(bsMer.getMerNo());
            merPayConfig.setPayPlatform(body.getPayPlatform());
            merPayConfig.setPayMerNo(body.getPayMerNo());
            merPayConfig.setPayMerKey(body.getPayMerKey());
            merPayConfig.setHltMerNo(body.getHltMerNo());
            merPayConfig.setHltInstCode(body.getHltInstCode());
            merPayConfig.setStatus(1);


            if (merPayConfig.getId() != null) {
                bsMerPayConfigService.update(merPayConfig);
            } else {
                bsMerPayConfigService.create(merPayConfig);
            }

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

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

    }


    @RequestMapping(value="/queryMerPayConfig",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户支付参数")
    public ResponseData queryMerPayConfig(@RequestParam(value = "merNo" , required = true) String merNo) {
        try {

            Map<String,Object> param = new HashMap<>();
            param.put("merNo", merNo);

            return ResponseMsgUtil.success(bsMerPayConfigService.getList(param));

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

    @RequestMapping(value="/delete",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除")
    public ResponseData delete(@RequestParam(value = "id" , required = false) Long id) {
        try {

            bsMerPayConfigService.delete(id , false);

            return ResponseMsgUtil.success("删除成功");

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

}
