package com.ruoyi.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.TKufang;
import com.ruoyi.system.service.ITKufangService;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TGoods;
import com.ruoyi.system.service.ITGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物品Controller
 * 
 * @author ruoyi
 * @date 2024-02-20
 */
@RestController
@RequestMapping("/system/goods")
public class TGoodsController extends BaseController
{
    @Autowired
    private ITGoodsService tGoodsService;

    /**
     * 查询物品列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(TGoods tGoods)
    {
        tGoods.setLoginUserId(getUserId());
        startPage();
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        return getDataTable(list);
    }

    /**
     * 导出物品列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @Log(title = "物品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TGoods tGoods)
    {
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        ExcelUtil<TGoods> util = new ExcelUtil<TGoods>(TGoods.class);
        util.exportExcel(response, list, "物品数据");
    }

    /**
     * 获取物品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tGoodsService.selectTGoodsById(id));
    }

    /**
     * 新增物品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "物品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TGoods tGoods)
    {
        tGoods.setCreateBy(getUsername());
        return toAjax(tGoodsService.insertTGoods(tGoods));
    }

    /**
     * 修改物品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "物品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TGoods tGoods)
    {
        tGoods.setUpdateBy(getUsername());
        return toAjax(tGoodsService.updateTGoods(tGoods));
    }

    /**
     * 删除物品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "物品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tGoodsService.deleteTGoodsByIds(ids));
    }

    @GetMapping(value = "getKuFangList")
    public AjaxResult getKuFangList()
    {
        return success(tGoodsService.getKuFangList(getUserId()));
    }

    @Autowired
    private ITKufangService tKufangService;

    @Anonymous
    @GetMapping("/getGoodsList")
    public AjaxResult getGoodsList(TGoods tGoods)
    {
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        return success(list);
    }

    @Anonymous
    @GetMapping(value = "getOpenKuFangList")
    public AjaxResult getOpenKuFangList(Long userId)
    {
        return success(tGoodsService.getKuFangList(userId));
    }

    @Anonymous
    @GetMapping(value = "getKuFangGoodsById")
    public AjaxResult getKuFangGoodsById(Long id)
    {
        Map<String, Object> param = new HashMap<>();
        TGoods goods = tGoodsService.selectTGoodsById(id);
        param.put("goods", goods);
        if (goods != null) {
            TKufang kufang = tKufangService.selectTKufangById(goods.getKufangId());
            param.put("kufang", kufang);
            List<Map<String, Object>> list = tKufangService.getUserByRole();
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = list.get(i);
                if (String.valueOf(kufang.getKfAdmin()).equals(String.valueOf(map.get("userId")))) {
                    param.put("adminName", map.get("userName"));
                    break;
                }
            }
        }
        return success(param);
    }
}
