package org.dromara.payment.merchant.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.payment.merchant.domain.bo.BusMerchantBo;
import org.dromara.payment.merchant.domain.vo.BusMerchantVo;
import org.dromara.payment.merchant.service.IBusMerchantService;
import org.dromara.payment.sys.domain.vo.SelectSydVo;
import org.dromara.payment.sys.service.ISysSydService;
import org.dromara.system.api.model.LoginUser;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 商户
 * 前端访问路由地址为:/payment/merchant
 *
 * @author LionLi
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/merchant")
public class BusMerchantController extends BaseController {

    private final IBusMerchantService busMerchantService;

    private final ISysSydService sysSydService;

    /**
     * 查询商户列表
     */
    @SaCheckPermission("payment:merchant:list")
    @GetMapping("/list")
    public TableDataInfo<BusMerchantVo> list(BusMerchantBo bo, PageQuery pageQuery) {
        return busMerchantService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商户列表
     */
    @SaCheckPermission("payment:merchant:export")
    @Log(title = "商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusMerchantBo bo, HttpServletResponse response) {
        List<BusMerchantVo> list = busMerchantService.queryList(bo);
        ExcelUtil.exportExcel(list, "商户", BusMerchantVo.class, response);
    }

    /**
     * 获取商户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("payment:merchant:query")
    @GetMapping("/{id}")
    public R<BusMerchantVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(busMerchantService.queryById(id));
    }

    /**
     * 新增商户
     */
    @SaCheckPermission("payment:merchant:add")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusMerchantBo bo) {
        return toAjax(busMerchantService.insertByBo(bo));
    }

    public static void main(String[] args) {
        BusMerchantBo bo = new BusMerchantBo();
        bo.setName("企业名称");
        bo.setNameNc("企业简称");
        bo.setPassage("结算通道");
        bo.setNsrSbh("统一社会信用代码");
        bo.setFr("法人");
        bo.setFrSfz("法人身份证");
        bo.setFrSjh("法人手机号");
        bo.setWtr("经办人");
        bo.setWtrSfz("经办人身份证");
        bo.setWtrSjh("经办人手机号");
        bo.setWtrEmail("经办人身份证");
        bo.setUnitArea("区域编码");
        bo.setUnitAreaName("区域名称");
        bo.setAddress("实际经营地址");
        bo.setNo("123");
        bo.setYyzzBeginDate(new Date());
        bo.setYyzzEndDate(new Date());
        bo.setIndustryCode("行业编码");
        bo.setIndustry("行业名称");
        bo.setFrCardidBeginDate(new Date());
        bo.setFrCardidEndDate(new Date());
        bo.setJbrCardidBeginDate(new Date());
        bo.setJbrCardidEndDate(new Date());
        bo.setYwyId(1L);
        bo.setKhm("账户名称");
        bo.setKhhCode("开户行code");
        bo.setKhh("开户行");
        bo.setKhd("开户支行");
        bo.setKhhNo("开户行行号/银联号");
        bo.setBankCard("银行账号");
        bo.setJbckzhbm("基本存款账户");
        bo.setInvoceHeader("发票抬头");
        bo.setInvoceCreditCode("发票纳税人识别号");
        bo.setInvoceBankName("发票开户银行");
        bo.setInvoceAddress("发票单位注册地址");
        bo.setInvoceBankCard("发票开户银行账号");
        bo.setInvoceDwdh("发票单位电话");
        bo.setYyzz("营业执照");
        bo.setFhxkzPic("开户许可证");
        bo.setFrSfzzm("法人身份证正面");
        bo.setFrSfzfm("法人身份证反面");
        bo.setJbrSfzzm("经办人身份证正面");
        bo.setJbrSfzfm("经办人身份证反面");
        bo.setMtqjzPic("门头照片");
        bo.setBgzp1Pic("办公照片1");
        bo.setBgzp2Pic("办公照片21");
        bo.setYwcjsm("业务场景说明");
        bo.setYwcjzsxcnh("业务场景真实性承诺函");
        bo.setFddsqxy("法大大授权协议");
        bo.setTshyxkz("特殊行业许可证");

        System.out.println(JSONUtil.toJsonStr(bo));
    }

    /**
     * 配置业务员
     */
    @SaCheckPermission("payment:merchant:setYwy")
    @Log(title = "配置业务员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/setYwy")
    public R<Integer> setYwy(Long id,Long ywyId) {
        return R.ok(busMerchantService.setYwy(id,ywyId));
    }


    /**
     * 解绑业务员
     */
    @SaCheckPermission("payment:merchant:unbindYwy")
    @Log(title = "解绑业务员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/ubYwy")
    public R<Integer> unbindYwy(Long id,Long ywyId) {
        return R.ok(busMerchantService.unbindYwy(id,ywyId));
    }







    /**
     * 修改商户
     */
    @SaCheckPermission("payment:merchant:edit")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusMerchantBo bo) {
        return toAjax(busMerchantService.updateByBo(bo));
    }

    /**
     * 删除商户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("payment:merchant:remove")
    @Log(title = "商户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busMerchantService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据用户名查询商户可用的服务商
     * @param userName 用户名
     * @return
     */
    @PostMapping("/getSydByUserName")
    public R<List<SelectSydVo>> getSydByUserName(@NotEmpty(message = "用户名不能为空") String userName) {
        return R.ok(sysSydService.getSydByUserName(userName));
    }

    /**
     * 获取商户相关的服务商
     * @return
     */
    @GetMapping("/getSyds")
    public R<List<SelectSydVo>> getSyds() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return R.ok(sysSydService.selectSydByMerId(loginUser.getBusId()));
    }


    /**
     * 审核代理商
     */
    @SaCheckPermission("payment:merchant:check")
    @Log(title = "审核商户", businessType = BusinessType.UPDATE)
    @PostMapping("/chk")
    public R<Boolean> chk(Long merId,Long checkType,String reason,String domain) {
        return R.ok(this.busMerchantService.checkMerChant(merId,checkType,reason,domain));
    }



    /**
     * 切换服务商
     * @param sydId 服务商id
     * @return
     */
    @GetMapping("/switchSyd")
    public R<Void> switchSyd(long sydId) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        loginUser.setSydId(sydId);
        LoginHelper.updateLoginUser(loginUser);
        return R.ok();
    }
}
