package org.dromara.personnel.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.core.exception.ServiceException;
import org.dromara.personnel.domain.bo.OpsChurukudanReq;
import org.dromara.personnel.domain.dto.OpsChurukudanCountDto;
import org.dromara.personnel.domain.dto.OpsChurukudanDayCountDto;
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.personnel.domain.vo.OpsChurukudanVo;
import org.dromara.personnel.domain.bo.OpsChurukudanBo;
import org.dromara.personnel.service.IOpsChurukudanService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 运维-物资-出入库单管理
 * 前端访问路由地址为:/personnel/churukudan
 *
 * @author LionLi
 * @date 2025-09-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/churukudan")
public class OpsChurukudanController extends BaseController {

    private final IOpsChurukudanService opsChurukudanService;

    /**
     * 查询运维-物资-出入库单管理列表
     */
    @SaCheckPermission("personnel:churukudan:list")
    @GetMapping("/list")
    public TableDataInfo<OpsChurukudanVo> list(OpsChurukudanReq bo, PageQuery pageQuery) {
        if (bo.getProjectId() == null) {
            throw new ServiceException("项目id不能为空！！！");
        }
        return opsChurukudanService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询本月出入库类型
     */
    @SaCheckPermission("personnel:churukudan:list")
    @GetMapping("/getChuRuKuCount")
    public R<OpsChurukudanCountDto> getChuRuKuCount(OpsChurukudanReq bo) {
        if (bo.getProjectId() == null) {
            throw new ServiceException("项目id不能为空！！！");
        }
        if (bo.getStartDate() == null || bo.getEndDate() == null) {
            throw new ServiceException("查询时间范围不能为空！！！");
        }
        if (bo.getStartDate().isAfter(bo.getEndDate())) {
            throw new ServiceException("查询开始时间不能大于结束时间！！！");
        }
        return R.ok(opsChurukudanService.getChuRuKuCount(bo));
    }
    /**
     * 查询出入库月中每天的出入库数量
     */
    @SaCheckPermission("personnel:churukudan:list")
    @GetMapping("/getChuRuKuDayCount")
    public R<OpsChurukudanDayCountDto> getChuRuKuDayCount(OpsChurukudanReq bo) {
        if (bo.getProjectId() == null) {
            throw new ServiceException("项目id不能为空！！！");
        }
        if (bo.getStartDate() == null || bo.getEndDate() == null) {
            throw new ServiceException("查询时间范围不能为空！！！");
        }
        if (bo.getStartDate().isAfter(bo.getEndDate())) {
            throw new ServiceException("查询开始时间不能大于结束时间！！！");
        }
        return R.ok(opsChurukudanService.getChuRuKuDayCount(bo));
    }

    /**
     * 导出运维-物资-出入库单管理列表
     */
    @SaCheckPermission("personnel:churukudan:export")
    @Log(title = "运维-物资-出入库单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OpsChurukudanReq bo, HttpServletResponse response) {
        if (bo.getProjectId() == null) {
            throw new ServiceException("项目id不能为空！！！");
        }
        List<OpsChurukudanVo> list = opsChurukudanService.queryList(bo);
        ExcelUtil.exportExcel(list, "运维-物资-出入库单管理", OpsChurukudanVo.class, response);
    }

    /**
     * 获取运维-物资-出入库单管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("personnel:churukudan:query")
    @GetMapping("/{id}")
    public R<OpsChurukudanVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(opsChurukudanService.queryById(id));
    }

    /**
     * 新增运维-物资-出入库单管理
     */
    @SaCheckPermission("personnel:churukudan:add")
    @Log(title = "运维-物资-出入库单管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OpsChurukudanBo bo) {
        return toAjax(opsChurukudanService.insertByBo(bo));
    }

    /**
     * 修改运维-物资-出入库单管理
     */
    @SaCheckPermission("personnel:churukudan:edit")
    @Log(title = "运维-物资-出入库单管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OpsChurukudanBo bo) {
        return toAjax(opsChurukudanService.updateByBo(bo));
    }

    /**
     * 删除运维-物资-出入库单管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("personnel:churukudan:remove")
    @Log(title = "运维-物资-出入库单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(opsChurukudanService.deleteWithValidByIds(List.of(ids), true));
    }
}
