package com.yanzhu.base.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import com.yanzhu.common.constant.Constants;
import com.yanzhu.common.core.domain.entity.SysUser;
import com.yanzhu.common.core.redis.RedisCache;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yanzhu.common.annotation.Log;
import com.yanzhu.common.core.controller.BaseController;
import com.yanzhu.common.core.domain.AjaxResult;
import com.yanzhu.common.enums.BusinessType;
import com.yanzhu.base.domain.BaseAssetsType;
import com.yanzhu.base.service.IBaseAssetsTypeService;
import com.yanzhu.common.utils.poi.ExcelUtil;
import com.yanzhu.common.core.page.TableDataInfo;

/**
 * 物资类型Controller
 * 
 * @author yanZhu
 * @date 2024-02-24
 */
@RestController
@RequestMapping("/base/assetsType")
public class BaseAssetsTypeController extends BaseController
{
    @Autowired
    private IBaseAssetsTypeService baseAssetsTypeService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询物资类型列表
     */
    @PreAuthorize("@ss.hasPermi('base:assetsType:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseAssetsType baseAssetsType)
    {
        startPage();
        List<BaseAssetsType> list = baseAssetsTypeService.selectBaseAssetsTypeList(baseAssetsType);
        return getDataTable(list);
    }

    /**
     * 导出物资类型列表
     */
    @PreAuthorize("@ss.hasPermi('base:assetsType:export')")
    @Log(title = "物资类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseAssetsType baseAssetsType)
    {
        List<BaseAssetsType> list = baseAssetsTypeService.selectBaseAssetsTypeList(baseAssetsType);
        ExcelUtil<BaseAssetsType> util = new ExcelUtil<BaseAssetsType>(BaseAssetsType.class);
        util.exportExcel(response, list, "物资类型数据");
    }

    /**
     * 获取物资类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:assetsType:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(baseAssetsTypeService.selectBaseAssetsTypeById(id));
    }

    /**
     * 新增物资类型
     */
    @PreAuthorize("@ss.hasPermi('base:assetsType:add')")
    @Log(title = "物资类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseAssetsType baseAssetsType)
    {
        return toAjax(baseAssetsTypeService.insertBaseAssetsType(baseAssetsType));
    }

    /**
     * 修改物资类型
     */
    @PreAuthorize("@ss.hasPermi('base:assetsType:edit')")
    @Log(title = "物资类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseAssetsType baseAssetsType)
    {
        return toAjax(baseAssetsTypeService.updateBaseAssetsType(baseAssetsType));
    }

    /**
     * 删除物资类型
     */
    @PreAuthorize("@ss.hasPermi('base:assetsType:remove')")
    @Log(title = "物资类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseAssetsTypeService.deleteBaseAssetsTypeByIds(ids));
    }


    /**
     * 查询物资类型
     */
    @GetMapping("/findAllByCategory/{category}")
    public AjaxResult findAllByCategory(@PathVariable String category)
    {
        SysUser sysUser = super.getLoginUser().getUser();
        String key = "YANZHU.BASE.ASSETSTYPE.findAllByCategory." + sysUser.getParDeptId() + "." + category;
        Object object = redisCache.getCacheObject(key);
        if (object != null) {
            return success(object);
        }
        List<BaseAssetsType> list = baseAssetsTypeService.findAllByCategory(category);
        redisCache.setCacheObject(key, list, Constants.BASE_DATA_EXPIRATION, TimeUnit.MINUTES);
        return success(list);
    }

}
