package org.dromara.payment.sys.controller;

import java.io.IOException;
import java.util.*;

import cn.hutool.core.util.RandomUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.file.FileUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.payment.sys.domain.vo.SysSydFlowsVo;
import org.dromara.payment.sys.service.ISysSydFlowsService;
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.SysSydFlowsBo;
import org.dromara.common.mybatis.core.page.TableDataInfo;

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

    private final ISysSydFlowsService sysSydFlowsService;

    /**
     * 查询服务商流水列表
     */
    //@SaCheckPermission("payment:sydFlows:list")
    @GetMapping("/list")
    public TableDataInfo<SysSydFlowsVo> list(SysSydFlowsBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setSydId(loginUser.getBusId());
        return sysSydFlowsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出服务商流水列表
     */
    //@SaCheckPermission("payment:sydFlows:export")
    @Log(title = "服务商流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysSydFlowsBo bo, HttpServletResponse response,String fields) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setSydId(loginUser.getBusId());
        List fieldList = new ArrayList();
        if(StringUtils.isNotBlank(fields)){
            fieldList = Arrays.asList(fields.split(","));
        }
        List<SysSydFlowsVo> list = sysSydFlowsService.queryList(bo);
        ExcelUtil.exportExcelInclude(list, "服务商流水", SysSydFlowsVo.class, response,fieldList);
    }

    /**
     * 导出服务商交易流水B2B回单
     * @param ids 交易流水id(多个逗号分隔)
     */
    @Log(title = "导出服务商交易流水B2B回单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportB2B")
    public void exportB2B(@NotBlank(message = "交易流水id不能为空") String ids, HttpServletResponse response) throws IOException {
        SysSydFlowsBo where = new SysSydFlowsBo();
        where.setPayReceiptType(1);
        where.setParams(new HashMap<>());
        where.getParams().put("ids",ids);
        List<SysSydFlowsVo> details = sysSydFlowsService.queryList(where);
        List<String> b2bs = StreamUtils.toList(details,SysSydFlowsVo::getPayReceipt, b2b -> StringUtils.isNotBlank(b2b));
        FileUtils.downloadZip(DateUtils.dateTimeNow()+ RandomUtil.randomNumbers(8),response,b2bs);
    }

    /**
     * 导出服务商交易流水B2C回单
     * @param ids 交易流水id(多个逗号分隔)
     */
    @Log(title = "导出服务商交易流水B2C回单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportB2C")
    public void exportB2C(@NotBlank(message = "交易流水id不能为空") String ids, HttpServletResponse response) throws IOException {
        SysSydFlowsBo where = new SysSydFlowsBo();
        where.setPayReceiptType(2);
        where.setParams(new HashMap<>());
        where.getParams().put("ids",ids);
        List<SysSydFlowsVo> details = sysSydFlowsService.queryList(where);
        List<String> b2cs = StreamUtils.toList(details,SysSydFlowsVo::getPayReceipt, b2c -> StringUtils.isNotBlank(b2c));
        FileUtils.downloadZip(DateUtils.dateTimeNow()+ RandomUtil.randomNumbers(8),response,b2cs);
    }

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

    /**
     * 新增服务商流水
     */
    @SaCheckPermission("payment:sydFlows:add")
    @Log(title = "服务商流水", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysSydFlowsBo bo) {
        return toAjax(sysSydFlowsService.insertByBo(bo));
    }

    /**
     * 修改服务商流水
     */
    @SaCheckPermission("payment:sydFlows:edit")
    @Log(title = "服务商流水", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysSydFlowsBo bo) {
        return toAjax(sysSydFlowsService.updateByBo(bo));
    }

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