package com.willalp.web.controller.consume;

import cn.hutool.core.collection.CollectionUtil;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.consume.domain.ConsumeMaintain;
import com.willalp.consume.service.IConsumeMaintainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 订餐消费维护Controller
 *
 * @author willalp
 * @date 2021-12-02
 */
@RestController
@RequestMapping("/consume/maintain")
public class ConsumeMaintainController extends BaseController {
    @Autowired
    private IConsumeMaintainService consumeMaintainService;

    /**
     * 查询订餐消费维护列表
     */
    @PreAuthorize("@ss.hasPermi('consume:maintain:list')")
    @GetMapping("/list")
    public TableDataInfo list(ConsumeMaintain consumeMaintain) {
        startPage();
        List<ConsumeMaintain> list = consumeMaintainService.selectConsumeMaintainList(consumeMaintain);
        return getDataTable(list);
    }

    /**
     * 导出订餐消费维护列表
     */
    @PreAuthorize("@ss.hasPermi('consume:maintain:export')")
    @Log(title = "订餐消费维护", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ConsumeMaintain consumeMaintain) {
        List<ConsumeMaintain> list = consumeMaintainService.selectConsumeMaintainList(consumeMaintain);
        ExcelUtil<ConsumeMaintain> util = new ExcelUtil<ConsumeMaintain>(ConsumeMaintain.class);
        return util.exportExcel(list, "订餐消费维护数据");
    }

    /**
     * 获取订餐消费维护详细信息
     */
    @PreAuthorize("@ss.hasPermi('consume:maintain:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(consumeMaintainService.getById(id));
    }

    /**
     * 新增订餐消费维护
     */
    @PreAuthorize("@ss.hasPermi('consume:maintain:add')")
    @Log(title = "订餐消费维护", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConsumeMaintain consumeMaintain) {
        ConsumeMaintain con = new ConsumeMaintain();
        con.setMenuNumber(consumeMaintain.getMenuNumber());
        List<ConsumeMaintain> cons = consumeMaintainService.selectConsumeMaintainList(con);
        if (CollectionUtil.isNotEmpty(cons)){
            Date begin1 =DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",DateUtils.parseDateToStr("yyyy-MM-dd", consumeMaintain.getSendTime()) + " " + consumeMaintain.getBeginDate() + ":00");
            Date end1 =DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",DateUtils.parseDateToStr("yyyy-MM-dd", consumeMaintain.getSendTime()) + " " + consumeMaintain.getEndDate() + ":00");
            for (ConsumeMaintain s : cons) {
                Date begin2 =DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",DateUtils.parseDateToStr("yyyy-MM-dd", s.getSendTime()) + " " + s.getBeginDate() + ":00");
                Date end2 =DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",DateUtils.parseDateToStr("yyyy-MM-dd", s.getSendTime()) + " " + s.getEndDate() + ":00");
                if (DateUtils.checkOverlap(begin1,end1,begin2,end2)){
                    return AjaxResult.error("菜单使用时间发生重叠");
                }
            }

        }
        consumeMaintainService.saveInfo(consumeMaintain);
        return AjaxResult.success();
    }

    /**
     * 修改订餐消费维护
     */
    @PreAuthorize("@ss.hasPermi('consume:maintain:edit')")
    @Log(title = "订餐消费维护", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConsumeMaintain consumeMaintain) {
        consumeMaintain.setUpdateBy(SecurityUtils.getUsername());
        consumeMaintain.setUpdateTime(DateUtils.getNowDate());
        return toAjax(consumeMaintainService.updateById(consumeMaintain));
    }

    /**
     * 删除订餐消费维护
     */
    @PreAuthorize("@ss.hasPermi('consume:maintain:remove')")
    @Log(title = "订餐消费维护", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(consumeMaintainService.removeByIds(Arrays.asList(ids)));
    }
}
