package com.willalp.web.controller.canteen;

import com.willalp.canteen.domain.HsGiftType;
import com.willalp.canteen.service.IHsGiftTypeService;
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.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 礼品类型Controller
 *
 * @author willalp
 * @date 2022-12-19
 */
@RestController
@RequestMapping("/service/giftType")
public class HsGiftTypeController extends BaseController
{
    @Autowired
    private IHsGiftTypeService hsGiftTypeService;

    /**
     * 查询礼品类型列表
     */
    @GetMapping("/list")
    public AjaxResult list(HsGiftType hsGiftType)
    {
        List<HsGiftType> list = hsGiftTypeService.selectHsGiftTypeList(hsGiftType);
        return AjaxResult.success(list);
    }

    /**
     * 导出礼品类型列表
     */
    @Log(title = "礼品类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HsGiftType hsGiftType)
    {
        List<HsGiftType> list = hsGiftTypeService.selectHsGiftTypeList(hsGiftType);
        ExcelUtil<HsGiftType> util = new ExcelUtil<HsGiftType>(HsGiftType.class);
        try {
            util.exportExcel(response, list, "礼品类型数据");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取礼品类型详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(hsGiftTypeService.getById(id));
    }

    /**
     * 新增礼品类型
     */
    @Log(title = "礼品类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsGiftType hsGiftType) {
        return toAjax(hsGiftTypeService.save(hsGiftType));
    }

    /**
     * 修改礼品类型
     */
    @Log(title = "礼品类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsGiftType hsGiftType) {
        return toAjax(hsGiftTypeService.updateById(hsGiftType));
    }

    /**
     * 删除礼品类型
     */
    @Log(title = "礼品类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hsGiftTypeService.removeByIds(Arrays.asList(ids)));
    }
}
