package com.willalp.web.controller.canteen;

import com.willalp.canteen.domain.HsCanteenDiningPlace;
import com.willalp.canteen.service.IHsCanteenDiningPlaceService;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
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.List;

/**
 * 就餐地Controller
 *
 * @author songyu
 * @date 2022-01-07
 */
@RestController
@RequestMapping("/canteen/place")
public class HsCanteenDiningPlaceController extends BaseController
{
    @Autowired
    private IHsCanteenDiningPlaceService hsCanteenDiningPlaceService;

    @Autowired
    private IHsSysNumberRuleService hsSysNumberRuleService;

    /**
     * 查询就餐地列表
     */
    //@PreAuthorize("@ss.hasPermi('canteen:place:list')")
    @GetMapping("/list")
    public AjaxResult list(HsCanteenDiningPlace hsCanteenDiningPlace)
    {
        hsCanteenDiningPlace.setParentId(null);
        List<HsCanteenDiningPlace> list = hsCanteenDiningPlaceService.selectHsCanteenDiningPlaceList(hsCanteenDiningPlace);
        return AjaxResult.success(list);
    }


    @GetMapping("/integralExchangeGetPlace")
    public AjaxResult integralExchangeGetPlace(){
        return AjaxResult.success(hsCanteenDiningPlaceService.getPlaceByConfigKey(IHsCanteenDiningPlaceService.GIFT_PLACE_CONFIG_KEY));
    }

    /**
     * 导出就餐地列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:place:export')")
    @Log(title = "就餐地", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsCanteenDiningPlace hsCanteenDiningPlace)
    {
        List<HsCanteenDiningPlace> list = hsCanteenDiningPlaceService.selectHsCanteenDiningPlaceList(hsCanteenDiningPlace);
        ExcelUtil<HsCanteenDiningPlace> util = new ExcelUtil<HsCanteenDiningPlace>(HsCanteenDiningPlace.class);
        return util.exportExcel(list, "就餐地数据");
    }

    /**
     * 获取就餐地详细信息
     */
    @PreAuthorize("@ss.hasPermi('canteen:place:query')")
    @GetMapping(value = "/{placeId}")
    public AjaxResult getInfo(@PathVariable("placeId") Long placeId)
    {
        return AjaxResult.success(hsCanteenDiningPlaceService.getById(placeId));
    }

    /**
     * 新增就餐地
     */
    @PreAuthorize("@ss.hasPermi('canteen:place:add')")
    @Log(title = "就餐地", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsCanteenDiningPlace hsCanteenDiningPlace)
    {
        //编号
        NumberRuleReq numberRuleReq =new NumberRuleReq();
        numberRuleReq.setRuleId(NumberRuleEnum.PLACE.getValue());
        numberRuleReq.setInput(hsCanteenDiningPlace.getJgid());
        String number = hsSysNumberRuleService.getNumber(numberRuleReq);
        hsCanteenDiningPlace.setPlaceNumber(number);

        hsCanteenDiningPlace.setCreateBy(SecurityUtils.getUsername());
        hsCanteenDiningPlace.setCreateTime(DateUtils.getNowDate());
        if (hsCanteenDiningPlace.getParentId() != null && hsCanteenDiningPlace.getParentId() != 0) {
            HsCanteenDiningPlace parent = hsCanteenDiningPlaceService.selectById(hsCanteenDiningPlace.getParentId());
            hsCanteenDiningPlace.setAncestors(parent.getAncestors() + "," + hsCanteenDiningPlace.getParentId());
        } else {
            hsCanteenDiningPlace.setParentId(null);
            hsCanteenDiningPlace.setAncestors("0");
        }
        return toAjax(hsCanteenDiningPlaceService.save(hsCanteenDiningPlace));
    }

    /**
     * 修改就餐地
     */
    @PreAuthorize("@ss.hasPermi('canteen:place:edit')")
    @Log(title = "就餐地", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsCanteenDiningPlace hsCanteenDiningPlace)
    {
        hsCanteenDiningPlace.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(hsCanteenDiningPlaceService.updateById(hsCanteenDiningPlace));
    }

    /**
     * 删除就餐地
     */
    @PreAuthorize("@ss.hasPermi('canteen:place:remove')")
    @Log(title = "就餐地", businessType = BusinessType.DELETE)
	@DeleteMapping("/{placeIds}")
    public AjaxResult remove(@PathVariable Long placeIds)
    {
        return toAjax(hsCanteenDiningPlaceService.deleteHsCanteenDiningPlaceByPlaceId(placeIds));
    }
}
