package org.dromara.system.controller.pay;

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

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.AjaxResult;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.pay.bo.XiaoyuanPayBo;
import org.dromara.system.domain.pay.vo.XiaoyuanPayVo;
import org.dromara.system.domain.score.bo.ScoreDataBo;
import org.dromara.system.listener.ScoreDateUtil;
import org.dromara.system.listener.XiaoYuanPayUtil;
import org.dromara.system.service.pay.IXiaoyuanPayService;
import org.springframework.http.MediaType;
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.springframework.web.multipart.MultipartFile;


/**
 * 教职工工资条
 *
 * @author boman
 * @date 2023-09-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xiaoYuan/pay")
public class XiaoyuanPayController extends BaseController {

    private final IXiaoyuanPayService xiaoyuanPayService;

    /**
     * 查询教职工工资条列表
     */
    @SaCheckPermission("xiaoYuan:pay:list")
    @GetMapping("/list")
    public AjaxResult list(XiaoyuanPayBo bo, PageQuery pageQuery) {
        return AjaxResult.success(xiaoyuanPayService.queryPageList(bo, pageQuery));
    }



    /**
     * 教职工工资导入数据
     *
     * @param file          导入文件
     */
    @SaCheckPermission("xiaoYuan:data:importData")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        //获取到处理后的数据
        List<Map<String, String>> maps = XiaoYuanPayUtil.importExcel(file);
        //进行新增操作
        xiaoyuanPayService.importData(maps);
        return R.ok();
    }

    /**
     * 导出教职工工资条列表
     */
    @SaCheckPermission("xiaoYuan:pay:export")
    @Log(title = "教职工工资条", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XiaoyuanPayBo bo, HttpServletResponse response) {
        List<XiaoyuanPayVo> list = xiaoyuanPayService.queryList(bo);
        ExcelUtil.exportExcel(list, "教职工工资条", XiaoyuanPayVo.class, response);
    }

    /**
     * 获取教职工工资条详细信息
     *
     * @param payId 主键
     */
    @SaCheckPermission("xiaoYuan:pay:query")
    @GetMapping("/{payId}")
    public R<XiaoyuanPayVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long payId) {
        return R.ok(xiaoyuanPayService.queryById(payId));
    }

    /**
     * 新增教职工工资条
     */
    @SaCheckPermission("xiaoYuan:pay:add")
    @Log(title = "教职工工资条", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XiaoyuanPayBo bo) {
        return toAjax(xiaoyuanPayService.insertByBo(bo));
    }

    /**
     * 修改教职工工资条
     */
    @SaCheckPermission("xiaoYuan:pay:edit")
    @Log(title = "教职工工资条", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XiaoyuanPayBo bo) {
        return toAjax(xiaoyuanPayService.updateByBo(bo));
    }

    /**
     * 删除教职工工资条
     *
     * @param payIds 主键串
     */
    @SaCheckPermission("xiaoYuan:pay:remove")
    @Log(title = "教职工工资条", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{payIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] payIds) {
        return toAjax(xiaoyuanPayService.deleteWithValidByIds(List.of(payIds), true));
    }
}
