package com.bs.pm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import com.bs.pm.domain.PmCanteenPurchasePlan;
import com.bs.pm.service.IPmCanteenPurchasePlanService;
import javax.annotation.Resource;

/**
 * 食堂采购计划Controller
 *
 * @author bs
 * @date 2025-10-05
 */
@Api(tags = "食堂采购计划")
@RestController
@RequestMapping("/canteen/plan")
public class PmCanteenPurchasePlanController extends BaseController {
    @Resource
    private IPmCanteenPurchasePlanService pmCanteenPurchasePlanService;

    /**
     * 分页查询食堂采购计划列表
     */
    @ApiOperation("分页查询食堂采购计划列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(PmCanteenPurchasePlan pmCanteenPurchasePlan) {
        startPage();
        LambdaQueryWrapper<PmCanteenPurchasePlan> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmCanteenPurchasePlan);
        List<PmCanteenPurchasePlan> list = pmCanteenPurchasePlanService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询食堂采购计划列表
     */
    @ApiOperation("查询食堂采购计划列表")
    @GetMapping("/list")
    public AjaxResult list(PmCanteenPurchasePlan pmCanteenPurchasePlan) {
        LambdaQueryWrapper<PmCanteenPurchasePlan> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmCanteenPurchasePlan);
        List<PmCanteenPurchasePlan> list = pmCanteenPurchasePlanService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出食堂采购计划列表
     */
    @ApiOperation("导出食堂采购计划列表")
    @Log(title = "食堂采购计划导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmCanteenPurchasePlan pmCanteenPurchasePlan) {
        LambdaQueryWrapper<PmCanteenPurchasePlan> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmCanteenPurchasePlan);
        List<PmCanteenPurchasePlan> list = pmCanteenPurchasePlanService.list(queryWrapper);
        ExcelUtil<PmCanteenPurchasePlan> util = new ExcelUtil<PmCanteenPurchasePlan>(PmCanteenPurchasePlan. class);
        util.exportExcel(response, list, "食堂采购计划数据");
    }

    /**
     * 获取食堂采购计划详细信息
     */
    @ApiOperation("获取食堂采购计划详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pmCanteenPurchasePlanService.getById(id));
    }

    /**
     * 新增食堂采购计划
     */
    @ApiOperation("新增食堂采购计划")
    @Log(title = "食堂采购计划新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmCanteenPurchasePlan pmCanteenPurchasePlan) {
        return toAjax(pmCanteenPurchasePlanService.save(pmCanteenPurchasePlan));
    }

    /**
     * 修改食堂采购计划
     */
    @ApiOperation("修改食堂采购计划")
    @Log(title = "食堂采购计划修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmCanteenPurchasePlan pmCanteenPurchasePlan) {
        return toAjax(pmCanteenPurchasePlanService.updateById(pmCanteenPurchasePlan));
    }

    /**
     * 删除食堂采购计划
     */
    @ApiOperation("删除食堂采购计划")
    @Log(title = "食堂采购计划删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(pmCanteenPurchasePlanService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<PmCanteenPurchasePlan> queryWrapper,PmCanteenPurchasePlan pmCanteenPurchasePlan){


        //id
        if(null != (pmCanteenPurchasePlan.getId())){
            queryWrapper.eq(PmCanteenPurchasePlan::getId,pmCanteenPurchasePlan.getId());
        }

        //计划编号
        if(StringUtils.isNotEmpty(pmCanteenPurchasePlan.getPlanNo())){
            queryWrapper.eq(PmCanteenPurchasePlan::getPlanNo,pmCanteenPurchasePlan.getPlanNo());
        }

        //计划采购日期
        if(null != (pmCanteenPurchasePlan.getPurchaseDate())){
            queryWrapper.eq(PmCanteenPurchasePlan::getPurchaseDate,pmCanteenPurchasePlan.getPurchaseDate());
        }

        //预计用餐人数
        if(null != (pmCanteenPurchasePlan.getExpectedPersonCount())){
            queryWrapper.eq(PmCanteenPurchasePlan::getExpectedPersonCount,pmCanteenPurchasePlan.getExpectedPersonCount());
        }

        //历史消耗数据
        if(StringUtils.isNotEmpty(pmCanteenPurchasePlan.getHistoryConsumption())){
            queryWrapper.eq(PmCanteenPurchasePlan::getHistoryConsumption,pmCanteenPurchasePlan.getHistoryConsumption());
        }

        //申请人
        if(null != (pmCanteenPurchasePlan.getApplyUser())){
            queryWrapper.eq(PmCanteenPurchasePlan::getApplyUser,pmCanteenPurchasePlan.getApplyUser());
        }

        //申请日期
        if(null != (pmCanteenPurchasePlan.getApplyDate())){
            queryWrapper.eq(PmCanteenPurchasePlan::getApplyDate,pmCanteenPurchasePlan.getApplyDate());
        }

        //审核人
        if(null != (pmCanteenPurchasePlan.getAuditUser())){
            queryWrapper.eq(PmCanteenPurchasePlan::getAuditUser,pmCanteenPurchasePlan.getAuditUser());
        }

        //审核时间
        if(null != (pmCanteenPurchasePlan.getAuditDate())){
            queryWrapper.eq(PmCanteenPurchasePlan::getAuditDate,pmCanteenPurchasePlan.getAuditDate());
        }

        //状态
        if(StringUtils.isNotEmpty(pmCanteenPurchasePlan.getStatus())){
            queryWrapper.eq(PmCanteenPurchasePlan::getStatus,pmCanteenPurchasePlan.getStatus());
        }

        //备注
        if(StringUtils.isNotEmpty(pmCanteenPurchasePlan.getRemark())){
            queryWrapper.eq(PmCanteenPurchasePlan::getRemark,pmCanteenPurchasePlan.getRemark());
        }

        //删除标志（0代表存在 2代表删除）
        if(StringUtils.isNotEmpty(pmCanteenPurchasePlan.getDelFlag())){
            queryWrapper.eq(PmCanteenPurchasePlan::getDelFlag,pmCanteenPurchasePlan.getDelFlag());
        }

        //创建部门
        if(null != (pmCanteenPurchasePlan.getCreateDept())){
            queryWrapper.eq(PmCanteenPurchasePlan::getCreateDept,pmCanteenPurchasePlan.getCreateDept());
        }

        //创建人
        if(null != (pmCanteenPurchasePlan.getCreateBy())){
            queryWrapper.eq(PmCanteenPurchasePlan::getCreateBy,pmCanteenPurchasePlan.getCreateBy());
        }

        //创建时间
        if(null != (pmCanteenPurchasePlan.getCreateTime())){
            queryWrapper.eq(PmCanteenPurchasePlan::getCreateTime,pmCanteenPurchasePlan.getCreateTime());
        }

        //修改人
        if(null != (pmCanteenPurchasePlan.getUpdateBy())){
            queryWrapper.eq(PmCanteenPurchasePlan::getUpdateBy,pmCanteenPurchasePlan.getUpdateBy());
        }

        //修改时间
        if(null != (pmCanteenPurchasePlan.getUpdateTime())){
            queryWrapper.eq(PmCanteenPurchasePlan::getUpdateTime,pmCanteenPurchasePlan.getUpdateTime());
        }

        //租户ID
        if(StringUtils.isNotEmpty(pmCanteenPurchasePlan.getTenantId())){
            queryWrapper.eq(PmCanteenPurchasePlan::getTenantId,pmCanteenPurchasePlan.getTenantId());
        }

        }

}
