package com.propertyManagement.web.controller.pay;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.propertyManagement.common.annotation.Log;
import com.propertyManagement.common.annotation.RepeatSubmit;
import com.propertyManagement.common.core.controller.BaseController;
import com.propertyManagement.common.core.domain.PageQuery;
import com.propertyManagement.common.core.domain.R;
import com.propertyManagement.common.core.page.TableDataInfo;
import com.propertyManagement.common.core.validate.AddGroup;
import com.propertyManagement.common.core.validate.EditGroup;
import com.propertyManagement.common.enums.BusinessType;
import com.propertyManagement.common.enums.IsEnableType;
import com.propertyManagement.common.utils.poi.ExcelUtil;
import com.propertyManagement.pay.domain.CmsPayMerchant;
import com.propertyManagement.pay.domain.bo.CmsPayMerchantBo;
import com.propertyManagement.pay.domain.vo.CmsPayMerchantVo;
import com.propertyManagement.pay.service.IPayMerchantService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 支付商户
 *
 * @author liushuai
 * @date 2025-05-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/payMerchant")
public class PayMerchantController extends BaseController {

    private final IPayMerchantService iPayMerchantService;

    /**
     * 查询支付商户列表
     */
    @SaCheckPermission("pay:payMerchant:list")
    @GetMapping("/list")
    public TableDataInfo<CmsPayMerchantVo> list(CmsPayMerchantBo bo, PageQuery pageQuery) {
        return iPayMerchantService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出支付商户列表
     */
    @SaCheckPermission("pay:payMerchant:export")
    @Log(title = "支付商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmsPayMerchantBo bo, HttpServletResponse response) {
        List<CmsPayMerchantVo> list = iPayMerchantService.queryList(bo);
        ExcelUtil.exportExcel(list, "支付商户", CmsPayMerchantVo.class, response);
    }

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

    /**
     * 获取支付商户详细信息
     *
     * @param code 商户编号
     * @return 详细信息
     */
    @SaCheckPermission("cms:pay-merchant:query")
    @GetMapping(value = "/getByCode/{code}")
    public R getInfoByCode(@PathVariable("code") String code) {
        return R.ok(iPayMerchantService.getByCode(code));
    }


    /**
     * 修改支付商户
     *
     * @apiNote  同步商户配置信息到App应用配置
     *
     * @param cmsPayMerchant 参数
     * @return 结果
     */
    @SaCheckPermission("@ss.hasPermi('cms:pay-merchant:edit')")
    @PutMapping("/syncEdit")
    public R syncEdit(@RequestBody CmsPayMerchantBo cmsPayMerchant) {
        iPayMerchantService.syncEdit(cmsPayMerchant);
        return R.ok();
    }

    /**
     * 新增支付商户
     */
    @SaCheckPermission("pay:payMerchant:add")
    @Log(title = "支付商户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CmsPayMerchantBo bo) {
        return toAjax(iPayMerchantService.insertByBo(bo));
    }

    /**
     * 修改支付商户
     */
    @SaCheckPermission("pay:payMerchant:edit")
    @Log(title = "支付商户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CmsPayMerchantBo bo) {
        return toAjax(iPayMerchantService.updateByBo(bo));
    }

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


    /**
     * 锁定商户
     *
     * @param id 渠道ID
     * @return 结果
     */
    @SaCheckPermission("cms:payMerchant:lock")
    @Log(title = "短信渠道锁定", businessType = BusinessType.UPDATE)
    @PostMapping("lock/{id}")
    public R<Void> lock(@NotNull(message = "渠道ID不能为空") @PathVariable Long id) {
        CmsPayMerchantVo payMerchantVo = iPayMerchantService.queryById(id);
        if (null == payMerchantVo) {
            return R.fail("商户不存在");
        }
        payMerchantVo.setIsLock(IsEnableType.YES.getCode());
        CmsPayMerchantBo bo = BeanUtil.toBean(payMerchantVo, CmsPayMerchantBo.class);
        return toAjax(iPayMerchantService.updateByBo(bo) ? 1 : 0);
    }
}
