package org.dromara.payment.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.payment.sys.domain.bo.SysUnitBo;
import org.dromara.payment.sys.domain.vo.SysUnitVo;
import org.dromara.payment.sys.service.ISysUnitService;
import org.dromara.settlement.api.domain.UnitConfDetail;
import org.dromara.settlement.api.domain.UnitConfGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 单位基础信息
 * 前端访问路由地址为:/payment/unit
 *
 * @author LionLi
 * @date 2024-03-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/unit")
public class SysUnitController extends BaseController {

    private final ISysUnitService sysUnitService;

    /**
     * 查询单位基础信息列表
     */
    @SaCheckPermission("payment:unit:list")
    @GetMapping("/list")
    public TableDataInfo<SysUnitVo> list(SysUnitBo bo, PageQuery pageQuery) {
        return sysUnitService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出单位基础信息列表
     */
    @SaCheckPermission("payment:unit:export")
    @Log(title = "单位基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUnitBo bo, HttpServletResponse response) {
        List<SysUnitVo> list = sysUnitService.queryList(bo);
        ExcelUtil.exportExcel(list, "单位基础信息", SysUnitVo.class, response);
    }

    /**
     * 获取单位基础信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("payment:unit:query")
    @GetMapping("/{id}")
    public R<SysUnitVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysUnitService.queryById(id));
    }

    /**
     * 新增单位基础信息
     */
    @SaCheckPermission("payment:unit:add")
    @Log(title = "单位基础信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysUnitBo bo) {
        return toAjax(sysUnitService.insertByBo(bo));
    }

    /**
     * 修改单位基础信息
     */
    @SaCheckPermission("payment:unit:edit")
    @Log(title = "单位基础信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysUnitBo bo) {
        return toAjax(sysUnitService.updateByBo(bo));
    }

    /**
     * 删除单位基础信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("payment:unit:remove")
    @Log(title = "单位基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysUnitService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取单位配置大类下面的组信息
     * @param unit_id
     * @param class_alias
     * @return
     */
    @GetMapping(value = "/getConfGroupByUnit/{unit_id}/{class_alias}")
    public R<List<UnitConfGroup>> getConfGroupByUnit(@NotEmpty(message = "单位id不能为空") @PathVariable Long unit_id, @NotEmpty(message = "配置类别不能为空") @PathVariable String class_alias) {
        return R.ok(sysUnitService.getConfGroupByUnit(unit_id,class_alias));
    }

    /**
     * 根据配置组查询单位配置项明细
     * @param unit_id
     * @param conf_group
     * @return
     */
    @GetMapping(value = "/getConfDetailByUnit/{unit_id}/{conf_group}")
    public R<List<UnitConfDetail>> getConfDetailByUnit(@NotEmpty(message = "单位id不能为空") @PathVariable Long unit_id, @NotEmpty(message = "配置组不能为空") @PathVariable String conf_group) {
        return R.ok(sysUnitService.getConfDetailByUnit(unit_id,conf_group));
    }

    @Log(title = "编辑单位配置项", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/editConfDetail")
    public R<Void> editConfDetail(@RequestBody Map map) {
        List<UnitConfDetail> lendReco = JSONObject.parseArray(map.get("confs").toString(),UnitConfDetail.class);
        return toAjax(sysUnitService.editConfDetail(lendReco));
    }



}
