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

import java.util.List;

import cn.com.ty.lift.ud.dataBank.controller.query.LiftControlQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftBrandEntity;
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.ILiftControlService;
import cn.com.ty.lift.ud.dataBank.service.ILiftFileService;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.common.DelReq;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

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;

/**
 * 电梯系统 API
 * 
 * @author Xson
 * @date 2020-04-23
 */
@Api(tags = "电梯系统 API- 魏瑞锋")
@RestController
@RequestMapping("/api/liftControls")
public class LiftControlController extends BaseController<LiftControlEntity, LiftControlQuery> {

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

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

    @Override
    protected IService<LiftControlEntity> getService() {
        return liftControlService;
    }

    // 根据条件查询
    @PostMapping("/query-page")
    @ApiOperation(value = "分页查询")
    public RestResponse getCondition(@RequestBody LiftControlQuery request) {
        IPage<LiftControlEntity> list = getService().page(request.page(), request.condition());
        for (LiftControlEntity le : list.getRecords()) {
            LiftBrandEntity byId = liftBrandService.getById(le.getBrandId());
            le.setLiftBrandEntity(byId);
        }
        return RestResponse.success(list, "Queried successfully");
    }

    @PostMapping("/queryAll")
    @ApiOperation(value = "查询所有")
    public RestResponse queryAll() {
        List<LiftControlEntity> queryAll = liftControlService.queryAll();
        return RestResponse.success(queryAll, "Queried successfully");
    }

    // 根据id查详情
    @GetMapping("/query/{id}")
    @ApiOperation(value = "获取详情")
    public RestResponse get(@PathVariable Long id) {
        LiftControlEntity entity = getService().getById(id);
        LiftBrandEntity byId = liftBrandService.getById(entity.getBrandId());
        List<LiftFileEntity> listByDataIdAndDataTable = liftFileService.getListByDataIdAndDataTable(id, 3);
        entity.setLiftBrandEntity(byId);
        entity.setListFile(listByDataIdAndDataTable);
        return RestResponse.success(entity, "Queried successfully");
    }

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

    @ApiOperation(value = "批量下架")
    @PostMapping("noUseBath")
    public RestResponse noUseBath(@RequestBody DelReq ids) {
        liftControlService.updateByIds(ids.getIds(), 0);
        return RestResponse.success( "下架成功");
    }

    @ApiOperation(value = "批量首页显示")
    @PostMapping("showIndex")
    public RestResponse showIndex(@RequestBody DelReq ids) {
        liftControlService.updateTopFlagByIds(ids.getIds(), 1);
        return RestResponse.success("首页显示");
    }

    @ApiOperation(value = "取消批量首页显示")
    @PostMapping("noshowIndex")
    public RestResponse noshowIndex(@RequestBody DelReq ids) {
        liftControlService.updateTopFlagByIds(ids.getIds(), 0);
        return RestResponse.success( "取消首页显示");
    }
}