package cn.ezhizao.project.business.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.domain.BizInspectionCategory;
import cn.ezhizao.project.business.domain.BizInspectionInstruction;
import cn.ezhizao.project.business.service.IBizInspectionCategoryService;
import cn.ezhizao.project.business.service.IBizInspectionInstructionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
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
 * 日期 2023-11-14
 */
@RestController
@RequestMapping("/business/inspectionInstruction")
public class BizInspectionInstructionController extends BaseController {
    @Resource
    private IBizInspectionInstructionService iBizInspectionInstructionService;
    @Resource
    private IBizInspectionCategoryService iBizInspectionCategoryService;
    @Resource
    private HttpServletRequest request;

    @PostMapping("/listCategory")
    public TableDataInfo listCategory(@RequestBody Map<String, Object> data) throws NoSuchFieldException, IllegalAccessException {
        String tenantId = request.getHeader("tenantId");
        QueryWrapper<BizInspectionCategory> queryWrapper = new QueryWrapper<>();
        if (tenantId != null && !"0".equals(tenantId)) {
            queryWrapper.eq("tenant_id", Long.valueOf(tenantId));
        }
        if (data.get("type") != null && !"0".equals(data.get("type").toString())) {
            int type = Integer.parseInt(data.get("type").toString());
            queryWrapper.eq("type", type);
        }

        List<BizInspectionCategory> list = iBizInspectionCategoryService.list(queryWrapper);
        List<BizInspectionCategory> resultList = iBizInspectionCategoryService.recursionList(list, 0);
        return getDataTable(resultList);

    }

    /**
     * 获取检查指导书类别详细信息
     */
    @GetMapping(value = "/getCategoryInfo/{id}")
    public AjaxResult getCategoryInfo(@PathVariable("id") Long id) {
        String tenantId = request.getHeader("tenantId");
        Integer zero = 0;
        QueryWrapper<BizInspectionCategory> queryWrapper = new QueryWrapper<>();
        // 获取所有一级类别
        queryWrapper.eq("tenant_id", Long.valueOf(tenantId))
                    .eq("parent_id", zero);
        List<BizInspectionCategory> categoryList = iBizInspectionCategoryService.list(queryWrapper);
        BizInspectionCategory bizInspectionCategory = iBizInspectionCategoryService.getById(id);
        Map<String, Object> map = new HashMap<>(3);
        map.put("formData", bizInspectionCategory);
        map.put("parentOptions", iBizInspectionCategoryService.getCategoryOptions(categoryList));
        return success(map);
    }

    /**
     * 新增检查指导书类别
     */
    @Log(title = "检查指导书类别", businessType = BusinessType.INSERT)
    @PostMapping(value = "/saveCategory")
    public AjaxResult saveCategory(@RequestBody BizInspectionCategory bizInspectionCategory) throws NoSuchFieldException, IllegalAccessException {
        final String tenantId = request.getHeader("tenantId");
        bizInspectionCategory.setTenantId(Long.valueOf(tenantId));
        return toAjax(iBizInspectionCategoryService.saveOrUpdate(bizInspectionCategory));
    }

    /**
     * 删除检查指导书类别
     */
    @Log(title = "检查指导书类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeCategory/{ids}")
    public AjaxResult removeCategory(@PathVariable List<Long> ids) {
        List<BizInspectionInstruction> instructions = iBizInspectionInstructionService.query().in("category_id", ids).list();
        if (!instructions.isEmpty()) {
            return AjaxResult.error("该类别下存在检查指导书，不能删除");
        }
        return toAjax(iBizInspectionCategoryService.removeBatchByIds(ids));
    }

    /************************************************ 检查指导书 ************************************************/
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Map<String, Object> data) throws NoSuchFieldException, IllegalAccessException, JsonProcessingException {
        // 分页
        startPage();
        List<BizInspectionInstruction> list = iBizInspectionInstructionService.getList(data);
        return getDataTable(list);
    }

    /**
     * 获取检查指导书详细信息
     */
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(iBizInspectionInstructionService.getById(id));
    }

    /**
     * 新增检查指导书
     */
    @Log(title = "检查指导书", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save")
    public AjaxResult save(@RequestBody BizInspectionInstruction bizInspectionInstruction) throws NoSuchFieldException, IllegalAccessException {
        final String tenantId = request.getHeader("tenantId");
        bizInspectionInstruction.setTenantId(Long.valueOf(tenantId));
        return toAjax(iBizInspectionInstructionService.saveOrUpdate(bizInspectionInstruction));
    }

    /**
     * 删除检查指导书
     */
    @PreAuthorize("@ss.hasPermi('business:carrier:remove')")
    @Log(title = "检查指导书", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(iBizInspectionInstructionService.removeBatchByIds(ids));
    }
}
