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.CmsPayBizBo;
import com.propertyManagement.pay.domain.vo.CmsPayBizVo;
import com.propertyManagement.pay.service.IPayBizService;
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/payBiz")
public class PayBizController extends BaseController {

    private final IPayBizService iPayBizService;

    /**
     * 查询支付业务列表
     */
    @SaCheckPermission("pay:payBiz:list")
    @GetMapping("/list")
    public TableDataInfo<CmsPayBizVo> list(CmsPayBizBo bo, PageQuery pageQuery) {
        return iPayBizService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出支付业务列表
     */
    @SaCheckPermission("pay:payBiz:export")
    @Log(title = "支付业务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmsPayBizBo bo, HttpServletResponse response) {
        List<CmsPayBizVo> list = iPayBizService.queryList(bo);
        ExcelUtil.exportExcel(list, "支付业务", CmsPayBizVo.class, response);
    }

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

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

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

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

    /**
     * 锁定支付业务
     *
     * @param id 支付业务ID
     * @return 结果
     */
    @SaCheckPermission("pay:payBiz:lock")
    @Log(title = "锁定支付业务", businessType = BusinessType.UPDATE)
    @PostMapping("lock/{id}")
    public R<Void> lock(@NotNull(message = "支付业务ID不能为空") @PathVariable Long id) {
        CmsPayBizVo payBizVo = iPayBizService.queryById(id);
        if (null == payBizVo) {
            return R.fail("支付业务不存在");
        }
        payBizVo.setIsLock(IsEnableType.YES.getCode());
        CmsPayBizBo bo = BeanUtil.toBean(payBizVo, CmsPayBizBo.class);
        return toAjax(iPayBizService.updateByBo(bo) ? 1 : 0);
    }
}
