package com.gxwebsoft.apps.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.BcTemporaryService;
import com.gxwebsoft.apps.entity.BcTemporary;
import com.gxwebsoft.apps.param.BcTemporaryParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 临时报餐管理控制器
 *
 * @author 科技小王子
 * @since 2023-04-24 21:47:57
 */
@Api(tags = "临时报餐管理管理")
@RestController
@RequestMapping("/api/apps/bc-temporary")
public class BcTemporaryController extends BaseController {
    @Resource
    private BcTemporaryService bcTemporaryService;

    @PreAuthorize("hasAuthority('apps:bcTemporary:list')")
    @OperationLog
    @ApiOperation("分页查询临时报餐管理")
    @GetMapping("/page")
    public ApiResult<PageResult<BcTemporary>> page(BcTemporaryParam param) {
        // 使用关联查询
        return success(bcTemporaryService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:list')")
    @OperationLog
    @ApiOperation("查询全部临时报餐管理")
    @GetMapping()
    public ApiResult<List<BcTemporary>> list(BcTemporaryParam param) {
        // 使用关联查询
        return success(bcTemporaryService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:list')")
    @OperationLog
    @ApiOperation("根据id查询临时报餐管理")
    @GetMapping("/{id}")
    public ApiResult<BcTemporary> get(@PathVariable("id") Integer id) {
        return success(bcTemporaryService.getById(id));
        // 使用关联查询
        //return success(bcTemporaryService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:save')")
    @OperationLog
    @ApiOperation("添加临时报餐管理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody BcTemporary bcTemporary) {
        bcTemporary.setUserId(getLoginUserId());
        if (bcTemporaryService.save(bcTemporary)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }


    @PreAuthorize("hasAuthority('apps:bcTemporary:save')")
    @OperationLog
    @ApiOperation("申请临时报餐")
    @PostMapping("/apply")
    public ApiResult<?> apply(@RequestBody BcTemporary bcTemporary) {
      bcTemporary.setUserId(getLoginUserId());
      final int count = bcTemporaryService.count(new LambdaQueryWrapper<BcTemporary>()
        .eq(BcTemporary::getUserId, getLoginUserId())
        .eq(BcTemporary::getStatus, 0)
        .eq(BcTemporary::getDayTime,DateUtil.today()));
      if(count > 0){
        return fail("请勿重复提交");
      }
      final DateTime dayTime = DateUtil.parse(DateUtil.today());
      bcTemporary.setDayTime(dayTime);
      bcTemporary.setExpirationTime(DateUtil.offsetMinute(DateUtil.parse(DateUtil.now()), 60));
      if (bcTemporaryService.save(bcTemporary)) {
        return success("提交成功");
      }
      return fail("提交成功");
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:list')")
    @OperationLog
    @ApiOperation("修改临时报餐管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody BcTemporary bcTemporary) {
        // 失效时间为审核通过后半个小时
        bcTemporary.setExpirationTime(DateUtil.offsetMinute(DateUtil.parse(DateUtil.now()), 60));
        if (bcTemporaryService.updateById(bcTemporary)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:remove')")
    @OperationLog
    @ApiOperation("删除临时报餐管理")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (bcTemporaryService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:save')")
    @OperationLog
    @ApiOperation("批量添加临时报餐管理")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<BcTemporary> list) {
        if (bcTemporaryService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:update')")
    @OperationLog
    @ApiOperation("批量修改临时报餐管理")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<BcTemporary> batchParam) {
        if (batchParam.update(bcTemporaryService, "temporary_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:remove')")
    @OperationLog
    @ApiOperation("批量删除临时报餐管理")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (bcTemporaryService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
