package cn.ezhizao.project.business.equipment.controller;

import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.equipment.domain.BizEquipment;
import cn.ezhizao.project.business.equipment.service.IBizEquipmentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 设备档案Controller
 *
 * @author ezhizao
 * @date 2023-11-14
 */
@RestController
@RequestMapping("/business/equipment")
public class BizEquipmentController extends BaseController {
    @Resource
    private IBizEquipmentService iBizEquipmentService;
    @Resource
    HttpServletRequest request;

    /**
     * 查询设备档案列表
     */
    @PreAuthorize("@ss.hasPermi('business:equipment:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Map<String, Object> data) throws NoSuchFieldException, IllegalAccessException {
        Long categoryId = Long.valueOf(data.get("categoryId").toString());
//        Integer zero = 0;
//        final String tenantId = request.getHeader("tenantId");
        String keywords = data.get("keywords").toString();
        QueryWrapper<BizEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("equipment_name", keywords);
        queryWrapper.eq("equipment_category_id", categoryId);
        queryWrapper.eq("deleted", 0);
//        if (tenantId != null && !zero.toString().equals(tenantId)) {
//            queryWrapper.eq("tenant_id", Long.valueOf(tenantId));
//        }
        startPage();
        List<BizEquipment> list = iBizEquipmentService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 获取设备档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:equipment:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(iBizEquipmentService.getById(id));
    }

    /**
     * 新增设备档案
     */
//    @PreAuthorize("@ss.hasPermi('business:equipment:add, business:equipment:edit')")
    @Log(title = "设备档案", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save")
    public AjaxResult save(@RequestBody BizEquipment bizEquipment) throws NoSuchFieldException, IllegalAccessException {
//        final String tenantId = request.getHeader("tenantId");
//        bizEquipment.setTenantId(Long.valueOf(tenantId));
        return toAjax(iBizEquipmentService.saveOrUpdate(bizEquipment));
    }

    /**
     * 删除设备档案
     */
    @PreAuthorize("@ss.hasPermi('business:equipment:remove')")
    @Log(title = "设备档案", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(iBizEquipmentService.removeBatchByIds(ids));
    }
}
