package cn.ezhizao.project.business.carrier.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.carrier.domain.BizCarrier;
import cn.ezhizao.project.business.carrier.domain.BizCarrierCategory;
import cn.ezhizao.project.business.carrier.service.IBizCarrierCategoryService;
import cn.ezhizao.project.business.carrier.service.IBizCarrierService;
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/carrierCategory")
public class BizCarrierCategoryController extends BaseController {
    @Resource
    private HttpServletRequest request;

    @Resource
    private IBizCarrierCategoryService iBizCarrierCategoryService;

    @Resource
    private IBizCarrierService carrierService;

    /**
     * 查询载具类别列表
     */
    @PreAuthorize("@ss.hasPermi('business:carrier:list')")
    @Log(title = "载具类别查询", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BizCarrierCategory bizCarrierCategory) throws NoSuchFieldException, IllegalAccessException {
        Integer zero = 0;
//        String tenantId = request.getHeader("tenantId");

        QueryWrapper<BizCarrierCategory> queryWrapper = new QueryWrapper<>();
        // 获取所有一级类别
        queryWrapper.eq("parent_id", zero);
//        if (tenantId != null && !zero.toString().equals(tenantId)) {
//            queryWrapper.eq("tenant_id", Long.valueOf(tenantId));
//        }
        List<BizCarrierCategory> list = iBizCarrierCategoryService.list(queryWrapper);
        iBizCarrierCategoryService.getChildren(list);
        return getDataTable(list);

    }

    /**
     * 获取载具类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:carrier:query')")
    @Log(title = "载具类别查询详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        String tenantId = request.getHeader("tenantId");
        Integer zero = 0;
        QueryWrapper<BizCarrierCategory> queryWrapper = new QueryWrapper<>();
        // 获取所有一级类别
        queryWrapper.eq("deleted", zero)
                .eq("parent_id", zero);
//        queryWrapper.eq("tenant_id", Long.valueOf(tenantId));
        List<BizCarrierCategory> categoryList = iBizCarrierCategoryService.list(queryWrapper);
        BizCarrierCategory bizEquipmentDetail = iBizCarrierCategoryService.getById(id);


        Map<String, Object> map = new HashMap<>(3);
        map.put("formData", bizEquipmentDetail);
        map.put("parentOptions", iBizCarrierCategoryService.getCategoryOptions(categoryList));
        return success(map);
    }

    /**
     * 新增载具类别
     */
//    @PreAuthorize("@ss.hasPermi('business:carrier:save')")
    @Log(title = "载具类别新增", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save")
    public AjaxResult save(@RequestBody BizCarrierCategory bizCarrierCategory) throws NoSuchFieldException, IllegalAccessException {
        final String tenantId = request.getHeader("tenantId");
        bizCarrierCategory.setTenantId(Long.valueOf(tenantId));
        return toAjax(iBizCarrierCategoryService.saveOrUpdate(bizCarrierCategory));
    }

    /**
     * 删除载具类别
     */
    @PreAuthorize("@ss.hasPermi('business:carrier:remove')")
    @Log(title = "载具类别删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        List<BizCarrier> categoryId = carrierService.query().in("category_id", ids).list();
        if (categoryId.size() > 0) {
            return AjaxResult.error("该类别下存在载具，不能删除");
        }
        return toAjax(iBizCarrierCategoryService.removeBatchByIds(ids));
    }
}
