package com.willalp.web.controller.canteen;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.canteen.domain.HsCanteenCuisine;
import com.willalp.canteen.service.IHsCanteenCuisineService;
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.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
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.List;
import java.util.Map;

/**
 * cuisineController
 *
 * @author songyu
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/canteen/cuisine")
public class HsCanteenCuisineController extends BaseController {
    @Autowired
    private IHsCanteenCuisineService hsCanteenCuisineService;

    @Autowired
    private IHsSysNumberRuleService hsSysNumberRuleService;

    /**
     * 查询cuisine列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:cuisine:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsCanteenCuisine hsCanteenCuisine) {
        startPage();
        List<HsCanteenCuisine> list = hsCanteenCuisineService.selectHsCanteenCuisineList(hsCanteenCuisine);
        return getDataTable(list);
    }

    /**
     * 导出cuisine列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:cuisine:export')")
    @Log(title = "cuisine", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsCanteenCuisine hsCanteenCuisine) {
        List<HsCanteenCuisine> list = hsCanteenCuisineService.selectHsCanteenCuisineList(hsCanteenCuisine);
        ExcelUtil<HsCanteenCuisine> util = new ExcelUtil<HsCanteenCuisine>(HsCanteenCuisine.class);
        return util.exportExcel(list, "cuisine数据");
    }

    /**
     * 获取cuisine详细信息
     */
    @PreAuthorize("@ss.hasPermi('canteen:cuisine:query')")
    @GetMapping(value = "/{cuisineId}")
    public AjaxResult getInfo(@PathVariable("cuisineId") String cuisineId) {
        return AjaxResult.success(hsCanteenCuisineService.getById(cuisineId));
    }

    /**
     * 新增cuisine
     */
    @PreAuthorize("@ss.hasPermi('canteen:cuisine:add')")
    @Log(title = "cuisine", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsCanteenCuisine hsCanteenCuisine) {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsCanteenCuisine byId = hsCanteenCuisineService.getById(idStr);
            if (null == byId) {
                hsCanteenCuisine.setCuisineId(idStr);
                break;
            }
        }
        hsCanteenCuisine.setCreateBy(SecurityUtils.getUsername());
        hsCanteenCuisine.setCreateTime(DateUtils.getNowDate());
        //编号
        NumberRuleReq numberRuleReq = new NumberRuleReq();
        numberRuleReq.setRuleId(NumberRuleEnum.CUISINE.getValue());
        String number = hsSysNumberRuleService.getNumber(numberRuleReq);
        hsCanteenCuisine.setCuisineNumber(number);
        return toAjax(hsCanteenCuisineService.save(hsCanteenCuisine));
    }

    /**
     * 修改cuisine
     */
    @PreAuthorize("@ss.hasPermi('canteen:cuisine:edit')")
    @Log(title = "cuisine", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsCanteenCuisine hsCanteenCuisine) {
        hsCanteenCuisine.setUpdateBy(SecurityUtils.getUsername());
        hsCanteenCuisine.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsCanteenCuisineService.updateById(hsCanteenCuisine));
    }

    /**
     * 删除cuisine
     */
    @PreAuthorize("@ss.hasPermi('canteen:cuisine:remove')")
    @Log(title = "cuisine", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cuisineIds}")
    public AjaxResult remove(@PathVariable String[] cuisineIds) {
        return toAjax(hsCanteenCuisineService.removeByIds(Arrays.asList(cuisineIds)));
    }

    @GetMapping("/regList")
    public TableDataInfo regList(@RequestParam Map params) {
        startPage();
        List<HsCanteenCuisine> list = hsCanteenCuisineService.selectRegList(params);
        return getDataTable(list);
    }

    @GetMapping("/miwp")
    public AjaxResult menuItemsWithPictures(@RequestParam Map params) {
        return AjaxResult.success(hsCanteenCuisineService.menuItemsWithPictures(params));
    }

}
