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.OpsCaigouPlanReq;
import org.dromara.personnel.domain.vo.OpsCaigouPlanChanpinListVo;
import org.dromara.personnel.domain.vo.OpsCaigouPlanChanpinVo;
import org.dromara.personnel.domain.vo.OpsCaigouPlanCountVo;
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.OpsCaigouPlanVo;
import org.dromara.personnel.domain.bo.OpsCaigouPlanBo;
import org.dromara.personnel.service.IOpsCaigouPlanService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 运维-物资-采购计划单
 * 前端访问路由地址为:/personnel/caigouPlan
 *
 * @author LionLi
 * @date 2025-09-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/caigouPlan")
public class OpsCaigouPlanController extends BaseController {

    private final IOpsCaigouPlanService opsCaigouPlanService;

    /**
     * 查询运维-物资-采购计划单列表
     */
    @SaCheckPermission("personnel:caigouPlan:list")
    @GetMapping("/list")
    public TableDataInfo<OpsCaigouPlanVo> list(OpsCaigouPlanReq bo, PageQuery pageQuery) {
        return opsCaigouPlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 出入库表单新增修改获取产品列表
     */
    @SaCheckPermission("personnel:caigouPlan:list")
    @GetMapping("/getChanpinList")
    public R<List<OpsCaigouPlanChanpinListVo>> getChanpinList(OpsCaigouPlanReq bo) {
        return R.ok(opsCaigouPlanService.getChanpinList(bo));
    }


    /**
     * 查询运维-物资-采购计划单数量统计
     */
    @SaCheckPermission("personnel:caigouPlan:getCount")
    @GetMapping("/getCount")
    public R<List<OpsCaigouPlanCountVo>> getCount(Long projectId) {
        if (projectId == null) {
            throw new ServiceException("项目id不能为空！！！");
        }
        return R.ok(opsCaigouPlanService.getCountVo(projectId));
    }

    /**
     * 查询运维-物资-采购计划单年度金额
     */
    @SaCheckPermission("personnel:caigouPlan:getJinE")
    @GetMapping("/getJinE")
    public R<OpsCaigouPlanVo> getJinE(Long projectId) {
        if (projectId == null) {
            throw new ServiceException("项目id不能为空！！！");
        }
        return R.ok(opsCaigouPlanService.getJinE(projectId));
    }



    /**
     * 导出运维-物资-采购计划单列表
     */
    @SaCheckPermission("personnel:caigouPlan:export")
    @Log(title = "运维-物资-采购计划单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OpsCaigouPlanReq bo, HttpServletResponse response) {
        List<OpsCaigouPlanVo> list = opsCaigouPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "运维-物资-采购计划单", OpsCaigouPlanVo.class, response);
    }

    /**
     * 获取运维-物资-采购计划单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("personnel:caigouPlan:query")
    @GetMapping("/{id}")
    public R<OpsCaigouPlanVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(opsCaigouPlanService.queryById(id));
    }

    /**
     * 新增运维-物资-采购计划单
     */
    @SaCheckPermission("personnel:caigouPlan:add")
    @Log(title = "运维-物资-采购计划单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OpsCaigouPlanBo bo) {
        return toAjax(opsCaigouPlanService.insertByBo(bo));
    }

    /**
     * 修改运维-物资-采购计划单
     */
    @SaCheckPermission("personnel:caigouPlan:edit")
    @Log(title = "运维-物资-采购计划单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OpsCaigouPlanBo bo) {
        return toAjax(opsCaigouPlanService.updateByBo(bo));
    }

    /**
     * 修改运维-物资-采购计划单设置采购金额
     */
    @SaCheckPermission("personnel:caigouPlan:edit")
    @Log(title = "运维-物资-采购计划单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/setJinE")
    public R<Void> setJinE(@RequestBody OpsCaigouPlanBo bo) {
        if (bo.getId() == null) {
            throw new ServiceException("id不能为空");
        }
        if (bo.getShijiJine() == null) {
            throw new ServiceException("采购金额不能为空");
        }

        return toAjax(opsCaigouPlanService.setJinE(bo));
    }

    /**
     * 修改运维-物资-采购计划单确认采购完成
     */
    @SaCheckPermission("personnel:caigouPlan:edit")
    @Log(title = "运维-物资-采购计划单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/editStatus")
    public R<Void> editStatus(@RequestBody OpsCaigouPlanBo bo) {
        if (bo.getId() == null) {
            throw new ServiceException("id不能为空");
        }
        return toAjax(opsCaigouPlanService.editStatus(bo));
    }

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