package com.willalp.web.controller.erms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.erms.domain.HsGoodsType;
import com.willalp.erms.service.IHsGoodsTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 物资类库Controller
 *
 * @author willalp
 * @date 2023-03-09
 */
@RestController
@RequestMapping("/erms/goodsType")
public class HsGoodsTypeController extends BaseController {
    @Resource
    private IHsGoodsTypeService hsGoodsTypeService;

    /**
     * 查询物资类库列表
     */
    @PreAuthorize("@ss.hasPermi('erms:goodsType:list')")
    @GetMapping("/list")
    public AjaxResult list(HsGoodsType hsGoodsType) {
        List<HsGoodsType> list = hsGoodsTypeService.selectHsGoodsTypeList(hsGoodsType);
        return AjaxResult.success(list);
    }

    /**
     * 查询物资类库列表
     * @param id
     */
    @GetMapping("/typeList")
    public AjaxResult getGoodsTypeList(String id) {
        List<HsGoodsType> list;
        if (StringUtils.isNotBlank(id)) {
            list = hsGoodsTypeService.getBaseMapper().selectList(new QueryWrapper<HsGoodsType>()
                    .eq("parent_id", id));
        } else {
            list = hsGoodsTypeService.getBaseMapper().selectList(new QueryWrapper<HsGoodsType>()
                    .eq("parent_id", "0"));
        }
        return AjaxResult.success(list);
    }

    /**
     * 导出物资类库列表
     */
    @PreAuthorize("@ss.hasPermi('erms:goodsType:export')")
    @Log(title = "物资类库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsGoodsType hsGoodsType) {
        List<HsGoodsType> list = hsGoodsTypeService.selectHsGoodsTypeList(hsGoodsType);
        ExcelUtil<HsGoodsType> util = new ExcelUtil<HsGoodsType>(HsGoodsType.class);
        return util.exportExcel(list, "物资类库数据");
    }

    /**
     * 获取物资类库详细信息
     */
    @PreAuthorize("@ss.hasPermi('erms:goodsType:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(hsGoodsTypeService.getById(id));
    }

    /**
     * 新增物资类库
     */
    @PreAuthorize("@ss.hasPermi('erms:goodsType:add')")
    @Log(title = "物资类库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsGoodsType hsGoodsType) {
        hsGoodsType.setCreateBy(SecurityUtils.getUsername());
        hsGoodsType.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsGoodsTypeService.save(hsGoodsType));
    }

    /**
     * 修改物资类库
     */
    @PreAuthorize("@ss.hasPermi('erms:goodsType:edit')")
    @Log(title = "物资类库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsGoodsType hsGoodsType) {
        hsGoodsType.setUpdateBy(SecurityUtils.getUsername());
        hsGoodsType.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsGoodsTypeService.updateById(hsGoodsType));
    }

    /**
     * 删除物资类库
     */
    @PreAuthorize("@ss.hasPermi('erms:goodsType:remove')")
    @Log(title = "物资类库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hsGoodsTypeService.removeByIds(Arrays.asList(ids)));
    }
}
