package com.fs.watch.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.exception.ServiceException;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.service.ICompanyRoleService;
import com.fs.company.service.ICompanyService;
import com.fs.system.service.ISysRoleService;
import com.fs.watch.domain.DeviceMaterials;
import com.fs.watch.domain.vo.DeviceMaterialsVo;
import com.fs.watch.service.DeviceMaterialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 设备物料信息Controller
 *
 * @author fs
 * @date 2024-08-26
 */
@RestController
@RequestMapping("/materials")
public class DeviceMaterialsController extends BaseController
{
    @Autowired
    private DeviceMaterialsService deviceMaterialsService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private ICompanyRoleService companyRoleService;

    /**
     * 查询设备物料信息列表
     */
//    @PreAuthorize("@ss.hasPermi('watchApi:materials:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceMaterials deviceMaterials, HttpServletRequest request)
    {

        //判断前端请求
//        String type = request.getHeader("x-frontend-type");
//        List<Integer> roleIds = null;
//        if ("admin".equals(type)){
//            //判断登录用户是角色
//            Long userId = getSysUserId();
//            if (userId == null){
//                throw new ServiceException("请先登录!");
//            }
//            roleIds = sysRoleService.selectRoleListByUserId(userId);
//        } else if ("company".equals(type)){
//            Long userId = getUserId();
//            if (userId == null){
//                throw new ServiceException("请先登录!");
//            }
//            roleIds = companyRoleService.selectRoleListByUserId(userId);
//        }
//        if ("doctor".equals(type)){
//
//        } else if (roleIds == null || roleIds.size() == 0){
//            throw new ServiceException("该用户没有分配角色");
//        }
//        Long companyId = 0L;
//        if (!(roleIds.contains(1) || roleIds.contains(2))) {
//            //按公司查询
//            Company company = companyService.selectCompanyByUserId(userId);
//            if (company == null){
//                throw new ServiceException("没有权限查看");
//            } else {
//                companyId = company.getCompanyId();
//            }
//        }
//        deviceMaterials.setCompanyId(companyId);
        startPage();
        List<DeviceMaterialsVo> list = deviceMaterialsService.selectDeviceMaterialsList(deviceMaterials);
        return getDataTable(list);
    }

    /**
     * 导出设备物料信息列表
     */
    @PreAuthorize("@ss.hasPermi('watchApi:materials:export')")
    @Log(title = "设备物料信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DeviceMaterials deviceMaterials)
    {
        List<DeviceMaterialsVo> list = deviceMaterialsService.selectDeviceMaterialsList(deviceMaterials);
        ExcelUtil<DeviceMaterialsVo> util = new ExcelUtil<DeviceMaterialsVo>(DeviceMaterialsVo.class);
        return util.exportExcel(list, "设备物料信息数据");
    }

    /**
     * 获取设备物料信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('watchApi:materials:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(deviceMaterialsService.selectDeviceMaterialsById(id));
    }

    /**
     * 新增设备物料信息
     */
    @PreAuthorize("@ss.hasPermi('watchApi:materials:add')")
    @Log(title = "设备物料信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceMaterials deviceMaterials)
    {
        return toAjax(deviceMaterialsService.insertDeviceMaterials(deviceMaterials));
    }

    /**
     * 修改设备物料信息
     */
    @PreAuthorize("@ss.hasPermi('watchApi:materials:edit')")
    @Log(title = "设备物料信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceMaterials deviceMaterials)
    {
        return toAjax(deviceMaterialsService.updateDeviceMaterials(deviceMaterials));
    }

    /**
     * 删除设备物料信息
     */
    @PreAuthorize("@ss.hasPermi('watchApi:materials:remove')")
    @Log(title = "设备物料信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceMaterialsService.deleteDeviceMaterialsByIds(ids));
    }
}
