/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.grainReserves.modular.gopsPlan.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.unis.grainReserves.modular.gopsPlan.param.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.unis.common.annotation.CommonLog;
import com.unis.common.pojo.CommonResult;
import com.unis.common.pojo.CommonValidList;
import com.unis.grainReserves.modular.gopsPlan.entity.GopsPlan;
import com.unis.grainReserves.modular.gopsPlan.service.GopsPlanService;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 购销计划控制器
 *
 * @author QSHan
 * @date  2024/05/18 17:25
 */
@Api(tags = "购销计划管理")
@ApiSupport(author = "UNIS_TEAM", order = 1)
@RestController
@Validated
public class GopsPlanController {

    @Resource
    private GopsPlanService gopsPlanService;

    /**
     * 获取购销计划分页
     *
     * @author QSHan
     * @date  2024/05/18 17:25
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取购销计划管理分页")
//    @SaCheckPermission("/grainReserves/gopsPlan/page")
    @GetMapping("/grainReserves/gopsPlan/page")
    public CommonResult<Page<GopsPlan>> page(GopsPlanPageParam gopsPlanPageParam) {
        return CommonResult.data(gopsPlanService.page(gopsPlanPageParam));
    }

    /**
     * 添加购销计划
     *
     * @author QSHan
     * @date  2024/05/18 17:25
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("添加购销计划")
    @CommonLog("添加购销计划")
//    @SaCheckPermission("/grainReserves/gopsPlan/add")
    @PostMapping("/grainReserves/gopsPlan/add")
    public CommonResult<String> add(@RequestBody @Valid GopsPlanAddParam gopsPlanAddParam) {
        gopsPlanService.add(gopsPlanAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑购销计划
     *
     * @author QSHan
     * @date  2024/05/18 17:25
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑购销计划")
    @CommonLog("编辑购销计划")
//    @SaCheckPermission("/grainReserves/gopsPlan/edit")
    @PostMapping("/grainReserves/gopsPlan/edit")
    public CommonResult<String> edit(@RequestBody @Valid GopsPlanEditParam gopsPlanEditParam) {
        gopsPlanService.edit(gopsPlanEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除购销计划
     *
     * @author QSHan
     * @date  2024/05/18 17:25
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除购销计划")
    @CommonLog("删除购销计划")
//    @SaCheckPermission("/grainReserves/gopsPlan/delete")
    @PostMapping("/grainReserves/gopsPlan/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<GopsPlanIdParam> gopsPlanIdParamList) {
        gopsPlanService.delete(gopsPlanIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取购销计划详情
     *
     * @author QSHan
     * @date  2024/05/18 17:25
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取购销计划详情")
//    @SaCheckPermission("/grainReserves/gopsPlan/detail")
    @GetMapping("/grainReserves/gopsPlan/detail")
    public CommonResult<GopsPlan> detail(@Valid GopsPlanIdParam gopsPlanIdParam) {
        return CommonResult.data(gopsPlanService.detail(gopsPlanIdParam));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("获取购销计划详情-列表参数")
//    @SaCheckPermission("/grainReserves/gopsPlan/detail")
    @GetMapping("/grainReserves/gopsPlan/getListDetail")
    public CommonResult<Map<String, Object>> getListDetail(@Valid GopsPlanDetail param) {
        return CommonResult.data(gopsPlanService.getListDetail(param));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation("计划进度查询-列表")
//    @SaCheckPermission("/grainReserves/gopsPlan/getPlanRatePage")
    @GetMapping("/grainReserves/gopsPlan/getPlanRatePage")
    public CommonResult<Page<List<Map<String, Object>>>> getPlanRatePage(@Valid GopsPlanPageParam param) {
        return CommonResult.data(gopsPlanService.getPlanRatePage(param));
    }

    @ApiOperationSupport(order = 8)
    @GetMapping("/grainReserves/gopsPlan/getEnumTreeList/{parentId}")
    @ApiOperation(value = "获取子级", notes = "子级数据(品种:1060,等级:1002,粮食性质:1032)")
    public CommonResult getEnumTreeList(@PathVariable("parentId") Integer parentId) {
        if (ObjectUtils.isEmpty(parentId))
            return CommonResult.data(Collections.EMPTY_LIST);
        return CommonResult.data(gopsPlanService.getEnumTreeList(parentId));
    }

    @ApiOperationSupport(order = 9)
    @GetMapping("/grainReserves/gopsPlan/getStoreHouseList/{orgId}")
    @ApiOperation(value = "获取仓房列表", notes = "获取组织下仓房列表")
    public CommonResult getStoreHouseList(@PathVariable("orgId") Integer orgId) {
        if (ObjectUtils.isEmpty(orgId))
            return CommonResult.data(Collections.EMPTY_LIST);
        return CommonResult.data(gopsPlanService.getStoreHouseList(orgId));
    }

    @ApiOperationSupport(order = 10)
    @GetMapping("/grainReserves/gopsPlan/getWarehouseList/{storehouseId}")
    @ApiOperation(value = "获取货位列表", notes = "获取货位列表")
    public CommonResult getWarehouseList(@PathVariable("storehouseId") Integer storehouseId) {
        if (ObjectUtils.isEmpty(storehouseId))
            return CommonResult.data(Collections.EMPTY_LIST);
        return CommonResult.data(gopsPlanService.getWarehouseList(storehouseId));
    }

    @ApiOperationSupport(order = 11)
    @GetMapping("/grainReserves/gopsPlan/getOrgIdList")
    @ApiOperation(value = "获取库级机构列表", notes = "获取库级机构列表")
    public CommonResult getOrgIdList() {
        return CommonResult.data(gopsPlanService.getOrgIdList());
    }

    @ApiOperationSupport(order = 12)
    @PostMapping("/grainReserves/gopsPlan/getCollectionsToUpdate")
    @ApiOperation(value = "批量下发", notes = "批量下发")
    public CommonResult getCollectionsToUpdate(@RequestBody List<GopsPlan> collections) {
        return CommonResult.data(gopsPlanService.getCollectionsToUpdate(collections));
    }

}
