package org.dromara.payment.sys.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.payment.sys.domain.SysSydSignContent;
import org.dromara.payment.sys.domain.vo.SysSydVo;
import org.dromara.payment.sys.service.ISysSydService;
import org.dromara.settlement.api.domain.SignZhifubao;
import org.dromara.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.payment.sys.domain.bo.SysSydBo;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 服务商
 * 前端访问路由地址为:/payment/syd
 *
 * @author LionLi
 * @date 2024-03-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/syd")
public class SysSydController extends BaseController {

    private final ISysSydService sysSydService;

    /**
     * 查询服务商列表
     */
    @SaCheckPermission("payment:syd:list")
    @GetMapping("/list")
    public TableDataInfo<SysSydVo> list(SysSydBo bo, PageQuery pageQuery) {
        return sysSydService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出服务商列表
     */
    @SaCheckPermission("payment:syd:export")
    @Log(title = "服务商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysSydBo bo, HttpServletResponse response) {
        List<SysSydVo> list = sysSydService.queryList(bo);
        ExcelUtil.exportExcel(list, "服务商", SysSydVo.class, response);
    }

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

    /**
     * 新增服务商
     */
    @SaCheckPermission("payment:syd:add")
    @Log(title = "服务商", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysSydBo bo) throws Exception {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setAddBy(loginUser.getNickname());
        return toAjax(sysSydService.insertByBo(bo));
    }

    /**
     * 修改服务商
     */
    @SaCheckPermission("payment:syd:edit")
    @Log(title = "服务商", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysSydBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setEditer(loginUser.getNickname());
        return toAjax(sysSydService.updateByBo(bo));
    }

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

    @Log(title = "开通虚拟户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/creatXNnum")
    public R<Void> creatXNnum(@NotNull(message = "服务商id不能为空") Long id,@NotEmpty(message = "服务商名称不能为空") String gsmc) {
        return toAjax(sysSydService.creatSydXNnum(id,gsmc));
    }

    /**
     * 验证是否配置结算中心
     * @author 张涛
     * @date 2021年9月9日
     * @param sydId 结算中心id
     * @return
     */
    @PostMapping("/valiConfig")
    public R<Boolean> valiConfig(@NotNull(message = "服务商id") Long sydId,@NotEmpty(message = "结算通道") String passage) {
        return R.ok(sysSydService.valiConfig(sydId,passage)!=null);
    }

    /**
     * 获取结算中心的自雇者签约信息
     * @author 张涛
     * @date 2022年9月8日
     * @param sydId 结算中心id
     * @return
     */
    @PostMapping("/getWorkerSignInfo")
    public R<SysSydSignContent> getWorkerSignInfo(@NotEmpty(message = "服务商id") int sydId) {
        return R.ok(sysSydService.getWorkerSignInfo(sydId));
    }

    /**
     * 保存结算中心的自雇者签约信息
     * @author 张涛
     * @date 2022年9月8日
     * @return
     */
    @Log(title = "保存结算中心的签约信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/saveWorkerSignInfo")
    public R<Void> saveWorkerSignInfo(@Validated(EditGroup.class) @RequestBody SysSydSignContent sign) {
        return toAjax(sysSydService.saveWorkerSignInfo(sign));
    }

    /**
     * 获取结算中心的支付宝签约信息
     * @author 张涛
     * @date 2022年9月8日
     * @param sydId 结算中心id
     * @return
     */
    @PostMapping("/getSignInfo")
    public R<SignZhifubao> getSignInfo(@NotNull(message = "服务商id") Long sydId, @NotEmpty(message = "结算通道") String passage) {
        return R.ok(sysSydService.getSignInfo(sydId,passage));
    }



    @GetMapping("/getInvInfo")
    @Log(title = "获取服务商的开票信息", businessType = BusinessType.SELECT)
    public R<SysSydVo>  getSydInvInfo(Long sydId){
        SysSydVo resultVo = new SysSydVo();
        SysSydVo sydVo = this.sysSydService.queryById(sydId);
        resultVo.setGsmc(sydVo.getGsmc());
        resultVo.setBankCard(sydVo.getBankCard());
        resultVo.setAddress(sydVo.getAddress());
        resultVo.setNsrSbh(sydVo.getNsrSbh());
        resultVo.setKfh(sydVo.getKfh());
        resultVo.setConPhone(sydVo.getConPhone());
        return R.ok(resultVo);
    }
}
