package com.willalp.web.controller.canteen;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.canteen.domain.vo.LiveUseMoneyMonthStatExcel;
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.enums.NumberRuleEnum;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.canteen.domain.HsCanteenOrder;
import com.willalp.canteen.domain.vo.MenuCountVo;
import com.willalp.canteen.service.IHsCanteenOrderService;
import com.willalp.number.domain.req.NumberRuleReq;
import com.willalp.number.service.IHsSysNumberRuleService;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 食堂订单Controller
 *
 * @author songyu
 * @date 2021-12-03
 */
@RestController
@RequestMapping("/canteen/order")
public class HsCanteenOrderController extends BaseController {
    @Autowired
    private IHsCanteenOrderService hsCanteenOrderService;
    @Autowired
    private IHsSysNumberRuleService hsSysNumberRuleService;


    /**
     * 查询食堂订单统计采购列表
     */
    @PostMapping("/payMiwp")
    public AjaxResult payMiwp(@RequestBody HsCanteenOrder order) {
        return toAjax(hsCanteenOrderService.payMiwp(order));
    }

    /**
     * 查询食堂订单统计采购列表
     */
    @GetMapping("/tjdcList")
    public AjaxResult tjdcList(@RequestParam("tjdcMenuCode") String tjdcMenuCode) {
        return AjaxResult.success(
                hsCanteenOrderService.getTheMenuItemsAndQuantitiesPerPerson(tjdcMenuCode));
    }

    /**
     * 查询食堂订单统计采购列表
     */
    @GetMapping("/jdcTj")
    public AjaxResult jdcTj(String menuCode) {
        return AjaxResult.success(
                hsCanteenOrderService.jcdStat(menuCode));
    }

    /**
     * 查询食堂订单统计采购列表
     */
    @GetMapping("/jdcMonthTj")
    public AjaxResult jdcMonthTj(int year,int month) {
        return AjaxResult.success(
                hsCanteenOrderService.jcdMonthStat(year, month));
    }

    /**
     * 查询食堂订单统计采购列表
     */
    @GetMapping("/useMoneyMonthTj")
    public AjaxResult useMoneyMonthTj(int year,int month) {
        return AjaxResult.success(
                hsCanteenOrderService.liveUseMoneyMonthStat(year, month));
    }

    /**
     * 导出食堂订单统计采购列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:export')")
    @Log(title = "食堂订单", businessType = BusinessType.EXPORT)
    @GetMapping("/useMoneyMonthTjExport")
    public AjaxResult useMoneyMonthTjExport(int year,int month) {
        List<LiveUseMoneyMonthStatExcel> list = hsCanteenOrderService.liveUseMoneyMonthStat(year, month);
        ExcelUtil<LiveUseMoneyMonthStatExcel> util = new ExcelUtil<>(LiveUseMoneyMonthStatExcel.class);
        return util.exportExcel(list, "【" + "现场消费月度统计表" + "】");
    }


    /**
     * 导出食堂订单统计采购列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:export')")
    @Log(title = "食堂订单", businessType = BusinessType.EXPORT)
    @GetMapping("/tjdcExport")
    public AjaxResult tjdcListExport(String tjdcMenuCode, String tjdcTiele) {
        List<MenuCountVo> list = hsCanteenOrderService.getTheMenuItemsAndQuantitiesPerPerson(tjdcMenuCode);
        ExcelUtil<MenuCountVo> util = new ExcelUtil<>(MenuCountVo.class);
        return util.exportExcel(list, "【" + tjdcTiele + "】");
    }


    /**
     * 查询食堂订单列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsCanteenOrder hsCanteenOrder) {
        startPage();
        List<HsCanteenOrder> list = hsCanteenOrderService.selectHsCanteenOrderList(hsCanteenOrder);
        return getDataTable(list);
    }

    /**
     * 导出食堂订单列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:export')")
    @Log(title = "食堂订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsCanteenOrder hsCanteenOrder) {
        List<HsCanteenOrder> list = hsCanteenOrderService.selectHsCanteenOrderList(hsCanteenOrder);
        ExcelUtil<HsCanteenOrder> util = new ExcelUtil<HsCanteenOrder>(HsCanteenOrder.class);
        return util.exportExcel(list, "食堂订单数据");
    }

    /**
     * 获取食堂订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId) {
        return AjaxResult.success(hsCanteenOrderService.getById(orderId));
    }

    /**
     * 新增食堂订单
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:add')")
    @Log(title = "食堂订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsCanteenOrder hsCanteenOrder) {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsCanteenOrder byId = hsCanteenOrderService.getById(idStr);
            if (null == byId) {
                hsCanteenOrder.setOrderId(idStr);
                break;
            }
        }
        //获取编号
        NumberRuleReq numberRuleReq = new NumberRuleReq();
        numberRuleReq.setRuleId(NumberRuleEnum.ORDER.getValue());
        numberRuleReq.setInput(String.valueOf(DateUtil.year(hsCanteenOrder.getPaytime())).substring(2, 4));
        String number = hsSysNumberRuleService.getNumber(numberRuleReq);
        hsCanteenOrder.setOrderNumber(number);
        return toAjax(hsCanteenOrderService.save(hsCanteenOrder));
    }

    /**
     * 修改食堂订单
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:edit')")
    @Log(title = "食堂订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsCanteenOrder hsCanteenOrder) {
        return toAjax(hsCanteenOrderService.updateById(hsCanteenOrder));
    }

    /**
     * 因错退还食堂订单
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:partback')")
    @Log(title = "食堂订单因错退还", businessType = BusinessType.UPDATE)
    @RequestMapping("/partback")
    @PostMapping
    public AjaxResult partback(@RequestBody HsCanteenOrder hsCanteenOrder) {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsCanteenOrder byId = hsCanteenOrderService.getById(idStr);
            if (null == byId) {
                hsCanteenOrder.setOrderId(idStr);
                break;
            }
        }
        if (hsCanteenOrder.getConsumeSign() != 0) {
            return AjaxResult.error("记账订单不可退还");
        }
        hsCanteenOrderService.checkOrderTotal(hsCanteenOrder);
        hsCanteenOrder.setZfbz(3);
        return toAjax(hsCanteenOrderService.save(hsCanteenOrder));
    }


    /**
     * 删除食堂订单
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:remove')")
    @Log(title = "食堂订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds) {
        return toAjax(hsCanteenOrderService.removeByIds(Arrays.asList(orderIds)));
    }

    /**
     * 作废食堂订单
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:back')")
    @RequestMapping("/back")
    @ResponseBody
    public AjaxResult back(@RequestBody HsCanteenOrder hsCanteenOrder) {
        return hsCanteenOrderService.getBack(hsCanteenOrder);
    }

    /**
     * 查询每日食堂订单量
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:lists')")
    @GetMapping("/lists")
    public Map lists(HsCanteenOrder hsCanteenOrder) {
        startPage();
        List<HsCanteenOrder> list = hsCanteenOrderService.selectHsCanteenOrderLists(hsCanteenOrder);
        Map map = new HashMap();
        if (list.size() > 0) {
            List<String> date = list.stream().filter(b -> 1 == 1).map(o -> (DateUtil.date(o.getPaytime()) + "").substring(0, 10)).collect(Collectors.toList());
            List<String> paylists = list.stream().filter(b -> 1 == 1).map(o -> o.getOrderId().toString()).collect(Collectors.toList());
            map.put("date", date);
            map.put("pay", paylists);
        }
        return map;
    }

    /**
     * 首页金额总数
     *
     * @param hsCanteenOrder
     * @return
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:sumpay')")
    @GetMapping("/sumpay")
    public String Sumpay(HsCanteenOrder hsCanteenOrder) {
        startPage();
        String fee = hsCanteenOrderService.getSumpay(hsCanteenOrder);
        return fee;
    }

    /**
     * 首页订单总数
     *
     * @param hsCanteenOrder
     * @return
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:sumordernum')")
    @GetMapping("/sumordernum")
    public String SumOrderNum(HsCanteenOrder hsCanteenOrder) {
        startPage();
        String total = hsCanteenOrderService.getSumOrderNum(hsCanteenOrder);
        return total;
    }


    @GetMapping("/getFinishOrder")
    public AjaxResult getFinishOrder(HsCanteenOrder hsCanteenOrder) {
        List<HsCanteenOrder> orders = hsCanteenOrderService.selectHsCanteenOrderList(hsCanteenOrder);
        orders = orders.stream().filter(o -> o.getZfbz() == 2).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(orders)) {
            return AjaxResult.error("无订单信息");
        }
        return AjaxResult.success(orders);
    }

}
