package cn.com.ty.lift.ud.dataBank.controller;

import java.util.Collection;
import java.util.List;

import cn.com.ty.lift.ud.common.DelReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.dataBank.controller.query.LiftCategoryQuery;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftBrandEntity;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftCategoryEntity;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftFileEntity;
import cn.com.ty.lift.ud.dataBank.service.ILiftBrandService;
import cn.com.ty.lift.ud.dataBank.service.ILiftCategoryService;
import cn.com.ty.lift.ud.dataBank.service.ILiftFileService;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 资料分类 API
 * 
 * @author Xson
 * @date 2020-04-23
 */
@Api(tags = "资料分类 API- 魏瑞锋")
@RestController
@RequestMapping("/api/liftCategorys")
public class LiftCategoryController extends BaseController<LiftCategoryEntity, LiftCategoryQuery> {

    private final Logger log = LoggerFactory.getLogger(LiftCategoryController.class);

    private @Autowired ILiftCategoryService liftCategoryService;

    private @Autowired ILiftFileService liftFileService;
    
    private @Autowired ILiftBrandService liftBrandService;

    @Override
    protected IService<LiftCategoryEntity> getService() {
        return liftCategoryService;
    }

    @PostMapping("/query-page")
    @ApiOperation(value = "分页查询")
    @Override
    public RestResponse getCondition(@RequestBody LiftCategoryQuery request) {
        return liftCategoryService.selectByExample(request);
    }

    @ApiOperation(value = "批量启用")
    @PostMapping("useBath")
    public RestResponse useBath(@RequestBody DelReq ids) {
        liftCategoryService.updateByIds(ids.getIds(), 1);
        return RestResponse.success("启用成功");
    }

    @ApiOperation(value = "批量关闭")
    @PostMapping("nouseBath")
    public RestResponse nouseBath(@RequestBody DelReq ids) {
        liftCategoryService.updateByIds(ids.getIds(), 0);
        return RestResponse.success("关闭成功");
    }

    @GetMapping("/queryToTree")
    @ApiOperation(value = "查询树状结构")
    public RestResponse queryToTree(Long brandId) {
        List<LiftCategoryEntity> queryToTree = liftCategoryService.queryToTree(brandId);
        for(LiftCategoryEntity lce : queryToTree) {
            LiftBrandEntity byId = liftBrandService.getById(lce.getBrandId());
            if(byId != null) {
                lce.setBrandName(byId.getName());
            }
        }
        return RestResponse.success(queryToTree, "请求成功");
    }

    // 根据id删除
    @DeleteMapping("/delete")
    @ApiOperation(value = "根据id删除")
    public RestResponse delete(@RequestBody DelReq req) {
        List<LiftCategoryEntity> listByPid =
            liftCategoryService.list(new QueryWrapper<LiftCategoryEntity>().eq("pid", req.getId()));
        if (!listByPid.isEmpty()) {
            return RestResponse.fail("该分类下有子级分类无法删除");
        }
        List<LiftFileEntity> listByCid =
            liftFileService.list(new QueryWrapper<LiftFileEntity>().eq("catagory_id", req.getId()));
        if (!listByCid.isEmpty()) {
            return RestResponse.fail("该子级分类下有相关资料无法删除");
        }
        boolean deleteFlag = getService().removeById(req.getId());
        if (deleteFlag)
            return RestResponse.success(null, "Deleted successfully");
        else
            return RestResponse.fail("Deleted unsuccessfully");
    }

    // 根据id批量删除
    @DeleteMapping("/delete-batch")
    @ApiOperation(value = "批量删除")
    public RestResponse deleteBath(@RequestBody DelReq req) {
        for (Long id : req.getIds()) {
            List<LiftCategoryEntity> listByPid =
                liftCategoryService.list(new QueryWrapper<LiftCategoryEntity>().eq("pid", id));
            if (!listByPid.isEmpty()) {
                return RestResponse.fail("该分类下有子级分类无法删除");
            }
            List<LiftFileEntity> listByCid =
                liftFileService.list(new QueryWrapper<LiftFileEntity>().eq("catagory_id", id));
            if (!listByCid.isEmpty()) {
                return RestResponse.fail("该子级分类下有相关资料无法删除");
            }
            getService().removeById(id);
        }
        return RestResponse.success(null, "Deleted successfully");
    }
}