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

import java.util.List;

import cn.com.ty.lift.ud.dataBank.controller.query.LiftFileQuery;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictDto;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictVo;
import cn.com.ty.lift.ud.tableDict.service.ITableDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.common.DelReq;
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.LiftControlEntity;
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.ILiftControlService;
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/liftFiles")
public class LiftFileController extends BaseController<LiftFileEntity, LiftFileQuery> {

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

    private @Autowired ILiftFileService liftFileService;
    private @Autowired ITableDictService iTableDictService;
    private @Autowired ILiftBrandService liftBrandService;
    private @Autowired ILiftCategoryService liftCategoryService;
    private @Autowired ILiftControlService iLiftControlService;

    @Override
    protected IService<LiftFileEntity> getService() {
        return liftFileService;
    }

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

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

    // 根据条件查询
    @PostMapping("/query-page")
    @ApiOperation(value = "分页查询")
    public RestResponse getCondition(@RequestBody LiftFileQuery request) {
        IPage<LiftFileEntity> list = liftFileService.selectByExample(request);
        // IPage<LiftFileEntity> list = getService().page(request.page(), request.condition());
        TableDictDto tdo = new TableDictDto();
        tdo.setTableName("lift_file");
        tdo.setField("data_table");
        List<TableDictVo> constantByTableAndColumn = iTableDictService.getConstantByTableAndColumn(tdo);
        for (LiftFileEntity le : list.getRecords()) {
            for (TableDictVo tableDictVo : constantByTableAndColumn) {
                if (tableDictVo.getKey().equals(le.getDataTable().toString())) {
                    le.setDataTableName(tableDictVo.getValue());
                }
            }
            LiftCategoryEntity byId2 = liftCategoryService.getById(le.getCatagoryId());
            if (byId2 != null) {
                le.setCatagoryName(byId2.getName());
            }
            LiftControlEntity byId3 = iLiftControlService.getById(le.getDataId());
            if (byId3 != null) {
                le.setControName(byId3.getName());
            }

        }
        return RestResponse.success(list, "Queried successfully");
    }

    // 根据id查详情
    @GetMapping("/query/{id}")
    @ApiOperation(value = "获取详情")
    public RestResponse get(@PathVariable Long id) {
        LiftFileEntity entity = getService().getById(id);
        if (entity != null) {
            entity.setBrowseNum(entity.getBrowseNum() == null ? 1 : entity.getBrowseNum() + 1);
            liftFileService.updateById(entity);
        }
        return RestResponse.success(entity, "Queried successfully");
    }

    @GetMapping("/updateDownLoadNum/{id}")
    @ApiOperation(value = "更新下载量")
    public RestResponse updateDownLoadNum(@PathVariable Long id) {
        LiftFileEntity entity = getService().getById(id);
        entity.setDownloadNum(entity.getDownloadNum() == null ? 1 : entity.getDownloadNum() + 1);
        liftFileService.updateById(entity);
        return RestResponse.success(null, "Queried successfully");
    }
}