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.domain.BizEquipmentCategory;
import cn.ezhizao.project.business.equipment.domain.BizEquipmentDetail;
import cn.ezhizao.project.business.equipment.service.IBizEquipmentCategoryService;
import cn.ezhizao.project.business.equipment.service.IBizEquipmentDetailService;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备档案Controller
 *
 * @author ezhizao
 * @date 2023-11-14
 */
@RestController
@RequestMapping("/business/equipmentCategory")
public class BizEquipmentCategoryController extends BaseController {

    @Resource
    private HttpServletRequest request;

    @Resource
    private IBizEquipmentCategoryService iBizEquipmentCategoryService;

    @Resource
    private IBizEquipmentDetailService bizEquipmentDetailService;

    /**
     * 查询设备档案列表
     */
    @PreAuthorize("@ss.hasAnyPermi('business:equipmentCategory:list,business:equipment:list')")
    @Log(title = "设备类别及明细查询", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BizEquipmentCategory equipmentCategory) throws NoSuchFieldException, IllegalAccessException {
          Integer zero = 0;
        String tenantId = request.getHeader("tenantId");
        QueryWrapper<BizEquipmentCategory> queryWrapper = new QueryWrapper<>();
        // 获取所有一级类别
        queryWrapper.eq("deleted", zero)
                .eq("parent_id", zero);
        if(equipmentCategory.getName()!=null) {
            queryWrapper.like("name", equipmentCategory.getName());
        }
        if (tenantId != null && !zero.toString().equals(tenantId)) {
            queryWrapper.eq("tenant_id", Long.valueOf(tenantId));
        }
        //startPage();
        List<BizEquipmentCategory> list = iBizEquipmentCategoryService.list(queryWrapper);
        iBizEquipmentCategoryService.getChildren(list);
        return getDataTable(list);
    }

    @PostMapping("/listCategories")
    @Log(title = "设备类别查询", businessType = BusinessType.SELECT)
    public AjaxResult listCategories(BizEquipmentCategory bizEquipmentCategory) throws NoSuchFieldException, IllegalAccessException {

        List<BizEquipmentCategory> list = bizEquipmentDetailService.getCategoryList(bizEquipmentCategory);
        return success(list);
    }

    /**
     * 获取设备档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:equipmentCategory:query')")
    @Log(title = "设备类别查询详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Integer zero = 0;
//        String tenantId = request.getHeader("tenantId");
        QueryWrapper<BizEquipmentCategory> queryWrapper = new QueryWrapper<>();
        // 获取所有一级类别
        queryWrapper.eq("deleted", zero)
                .eq("parent_id", zero);
//        if (tenantId != null && !zero.toString().equals(tenantId)) {
//            queryWrapper.eq("tenant_id", Long.valueOf(tenantId));
//        }
        List<BizEquipmentCategory> categoryList = iBizEquipmentCategoryService.list(queryWrapper);
        BizEquipmentCategory bizEquipmentDetail = iBizEquipmentCategoryService.getById(id);
        Map<String, Object> map = new HashMap<>(3);
        map.put("formData", bizEquipmentDetail);
        map.put("parentOptions", iBizEquipmentCategoryService.getCategoryOptions(categoryList));
        return success(map);
    }

    /**
     * 新增设备档案
     */
    @PreAuthorize("@ss.hasAnyPermi('business:equipmentCategory:add,business:equipment:add,business:equipmentCategory:edit')")
    @Log(title = "设备类别保存", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save")
    public AjaxResult save(@RequestBody BizEquipmentCategory bizEquipmentDetail) throws NoSuchFieldException, IllegalAccessException {
        final String tenantId = request.getHeader("tenantId");
        bizEquipmentDetail.setTenantId(Long.valueOf(tenantId));
        return toAjax(iBizEquipmentCategoryService.saveOrUpdate(bizEquipmentDetail));
    }

    /**
     * 删除设备档案
     */
    @PreAuthorize("@ss.hasAnyPermi('business:equipmentCategory:remove,business:equipment:remove')")
    @Log(title = "设备类别删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        List<BizEquipmentDetail> equipmentCategoryId = bizEquipmentDetailService.query().in("equipment_category_id", ids).list();
        if (equipmentCategoryId.size() > 0) {
            return AjaxResult.error("该类别下存在设备档案，不能删除");
        }
        return toAjax(iBizEquipmentCategoryService.removeBatchByIds(ids));
    }
}
