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.CmsPayProviderBo;
import com.propertyManagement.pay.domain.vo.CmsPayProviderVo;
import com.propertyManagement.pay.service.IPayProviderService;
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/payProvider")
public class PayProviderController extends BaseController {

    private final IPayProviderService iPayProviderService;

    /**
     * 查询支付服务商列表
     */
    @SaCheckPermission("pay:payProvider:list")
    @GetMapping("/list")
    public TableDataInfo<CmsPayProviderVo> list(CmsPayProviderBo bo, PageQuery pageQuery) {
        return iPayProviderService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询全部可用短信服务商列表
     *
     * @return 列表
     */
    @SaCheckPermission("cms:payProvider:list")
    @GetMapping("/listAll")
    public R<List<CmsPayProviderVo>> listAll() {
        CmsPayProviderBo payProviderBo = new CmsPayProviderBo();
        payProviderBo.setIsEnable(IsEnableType.YES.getCode());
        payProviderBo.setIsLock(IsEnableType.YES.getCode());
        List<CmsPayProviderVo> providerVoList = iPayProviderService.queryList(payProviderBo);
        return R.ok(providerVoList);
    }

    /**
     * 导出支付服务商列表
     */
    @SaCheckPermission("pay:payProvider:export")
    @Log(title = "支付服务商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmsPayProviderBo bo, HttpServletResponse response) {
        List<CmsPayProviderVo> list = iPayProviderService.queryList(bo);
        ExcelUtil.exportExcel(list, "支付服务商", CmsPayProviderVo.class, response);
    }

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

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

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

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

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