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.bo.CmsPayAppBo;
import com.propertyManagement.pay.domain.vo.CmsPayAppVo;
import com.propertyManagement.pay.service.IPayAppService;
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/payApp")
public class PayAppController extends BaseController {

    private final IPayAppService iPayAppService;

    /**
     * 查询支付应用列表
     */
    @SaCheckPermission("pay:payApp:list")
    @GetMapping("/list")
    public TableDataInfo<CmsPayAppVo> list(CmsPayAppBo bo, PageQuery pageQuery) {
        return iPayAppService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出支付应用列表
     */
    @SaCheckPermission("pay:payApp:export")
    @Log(title = "支付应用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmsPayAppBo bo, HttpServletResponse response) {
        List<CmsPayAppVo> list = iPayAppService.queryList(bo);
        ExcelUtil.exportExcel(list, "支付应用", CmsPayAppVo.class, response);
    }

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

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

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

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

    /**
     * 锁定应用配置
     *
     * @param id 应用ID
     * @return 结果
     */
    @SaCheckPermission("pay:payApp:lock")
    @Log(title = "短信渠道锁定", businessType = BusinessType.UPDATE)
    @PostMapping("lock/{id}")
    public R<Void> lock(@NotNull(message = "应用ID不能为空") @PathVariable Long id) {
        CmsPayAppVo payAppVo = iPayAppService.queryById(id);
        if (null == payAppVo) {
            return R.fail("应用不存在");
        }

        payAppVo.setIsLock(IsEnableType.YES.getCode());
        CmsPayAppBo bo = BeanUtil.toBean(payAppVo, CmsPayAppBo.class);
        return toAjax(iPayAppService.updateByBo(bo) ? 1 : 0);
    }
}
