package com.yeejoin.amos.boot.module.tzs.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.biz.common.utils.NameUtils;
import com.yeejoin.amos.boot.module.tzs.api.dto.ElevatorDto;
import com.yeejoin.amos.boot.module.tzs.api.dto.ElevatorNewDto;
import com.yeejoin.amos.boot.module.tzs.api.entity.Elevator;
import com.yeejoin.amos.boot.module.tzs.api.service.IElevatorService;
import com.yeejoin.amos.boot.module.tzs.biz.service.impl.ElevatorServiceImpl;
import com.yeejoin.amos.boot.module.tzs.biz.utils.BeanDtoVoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 电梯Api
 *
 * @author tb
 * @date 2021-06-01
 */
@RestController
@Api(tags = "电梯Api")
@RequestMapping(value = "/elevator")
public class ElevatorController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(ElevatorController.class);

    @Autowired
    IElevatorService iElevatorService;

    @Autowired
    ElevatorServiceImpl elevatorService;

    /**
     * 新增电梯
     *
     * @param elevatorDto 新增参数
     * @return 返回结果
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "新增电梯", notes = "新增电梯")
    public ResponseModel<com.yeejoin.amos.boot.module.tzs.api.dto.ElevatorDto> saveElevator(@RequestBody com.yeejoin.amos.boot.module.tzs.api.dto.ElevatorDto elevatorDto) {
        return ResponseHelper.buildResponse(elevatorService.saveElevator(elevatorDto));
    }

    /**
     * 根据id批量删除
     *
     * @param idList id
     * @return 返回结果
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除", notes = "根据id删除")
    public ResponseModel<Boolean> deleteById(@RequestParam String idList) {
        return ResponseHelper.buildResponse(elevatorService.deleteBatchBySeq(idList));
    }

    /**
     * 修改电梯
     *
     * @param elevatorDto 修改参数
     * @return 返回结果
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @RequestMapping(value = "/updateById", method = RequestMethod.PUT)
    @ApiOperation(httpMethod = "PUT", value = "修改电梯", notes = "修改电梯")
    public ResponseModel<com.yeejoin.amos.boot.module.tzs.api.dto.ElevatorDto> updateByIdElevator(@RequestBody com.yeejoin.amos.boot.module.tzs.api.dto.ElevatorDto elevatorDto) {
        return ResponseHelper.buildResponse(elevatorService.updateElevator(elevatorDto));
    }


    /**
     * 根据sequenceNbr查询
     *
     * @param sequenceNbr sequenceNbr
     * @return 返回结果
     */
    @TycloudOperation(needAuth = false, ApiLevel = UserType.AGENCY)
    @RequestMapping(value = "/{sequenceNbr}", method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "根据id查询", notes = "根据id查询")
    public ResponseModel<com.yeejoin.amos.boot.module.tzs.api.dto.ElevatorDto> selectById(@PathVariable Long sequenceNbr) {
        return ResponseHelper.buildResponse(elevatorService.selectBySeq(sequenceNbr));
    }

    /**
     * 列表分页查询
     *
     * @param pageNum     当前页
     * @param pageSize    每页大小
     * @param elevatorDto 查询参数
     * @return 返回结果
     */
    @TycloudOperation(needAuth = false, ApiLevel = UserType.AGENCY)
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "列表分页查询", notes = "列表分页查询")
    public ResponseModel<IPage<ElevatorDto>> listPage(String pageNum, String pageSize, com.yeejoin.amos.boot.module.tzs.api.dto.ElevatorDto elevatorDto) {
        Elevator elevator = BeanDtoVoUtils.convert(elevatorDto, Elevator.class);
        Page<Elevator> pageBean;
        QueryWrapper<Elevator> elevatorQueryWrapper = new QueryWrapper<>();
        Class<? extends Elevator> aClass = elevator.getClass();
        Arrays.stream(aClass.getDeclaredFields()).forEach(field -> {
            try {
                field.setAccessible(true);
                Object o = field.get(elevator);
                if (o != null) {
                    Class<?> type = field.getType();
                    String name = NameUtils.camel2Underline(field.getName());
                    if (type.equals(Integer.class)) {
                        Integer fileValue = (Integer) o;
                        elevatorQueryWrapper.eq(name, fileValue);
                    } else if (type.equals(Long.class)) {
                        Long fileValue = (Long) o;
                        elevatorQueryWrapper.eq(name, fileValue);
                    } else if (type.equals(String.class)) {
                        String fileValue = (String) o;
                        elevatorQueryWrapper.eq(name, fileValue);
                    } else {
                        String fileValue = (String) o;
                        elevatorQueryWrapper.eq(name, fileValue);
                    }
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        });
        IPage<Elevator> page;
        if (StringUtils.isBlank(pageNum) || StringUtils.isBlank(pageSize)) {
            pageBean = new Page<>(0, Long.MAX_VALUE);
        } else {
            pageBean = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        }
        page = iElevatorService.page(pageBean, elevatorQueryWrapper);
        IPage<ElevatorDto> elevatorVoIPage = BeanDtoVoUtils.elevatorIPageDto(page);
        return ResponseHelper.buildResponse(elevatorVoIPage);
    }


    /**
     * 相似电梯模糊查询
     *
     * @param elevatorNewDto 查询参数
     * @return 返回结果
     */
    @TycloudOperation(needAuth = false, ApiLevel = UserType.AGENCY)
    @RequestMapping(value = "/page/similar", method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "相似电梯模糊查询", notes = "相似电梯模糊查询")
    public ResponseModel<List<ElevatorDto>> similar(@RequestBody ElevatorNewDto elevatorNewDto) {
        Elevator elevator = BeanDtoVoUtils.convert(elevatorNewDto.getElevatorDto(), Elevator.class);
        QueryWrapper<Elevator> elevatorQueryWrapper = new QueryWrapper<>();
        Class<? extends Elevator> aClass = elevator.getClass();
        Arrays.stream(aClass.getDeclaredFields()).forEach(field -> {
            try {
                field.setAccessible(true);
                Object o = field.get(elevator);
                if (o != null) {
                    String name = NameUtils.camel2Underline(field.getName());
                    if ("rescue_code".equalsIgnoreCase(name)) {
                        Integer fileValue = (Integer) o;
                        elevatorQueryWrapper.like(name, fileValue);
                    } else if ("city".equalsIgnoreCase(name) || "district".equalsIgnoreCase(name)) {
                        String fileValue = (String) o;
                        elevatorQueryWrapper.eq(name, fileValue);
                    } else {
                        String fileValue = (String) o;
                        elevatorQueryWrapper.like(name, fileValue);
                    }
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        });
       List<Elevator> elevators =  iElevatorService.list(elevatorQueryWrapper);
       List<ElevatorDto> elevatorDtoList = new ArrayList<>();
        for (Elevator ele : elevators ) {
            ElevatorDto eleDto = BeanDtoVoUtils.convertElevatorToDto(ele,false);
            elevatorDtoList.add(eleDto);
        }
        return ResponseHelper.buildResponse(elevatorDtoList);
    }

    /**
     * 分页查询电梯信息
     *
     * @param pageNum     当前页
     * @param pageSize    每页大小
     * @param elevatorDto 查询参数
     * @return 查询结果
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @RequestMapping(value = "/query_elevator_list", method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "分页查询电梯信息", notes = "分页查询电梯信息")
    public ResponseModel<IPage<ElevatorDto>> queryElevatorList(String pageNum, String pageSize,
                                                               com.yeejoin.amos.boot.module.tzs.api.dto.ElevatorDto elevatorDto) {
        Elevator elevator = BeanDtoVoUtils.convert(elevatorDto, Elevator.class);
        Page<Elevator> pageBean;
        QueryWrapper<Elevator> elevatorQueryWrapper = new QueryWrapper<>();
        Class<? extends Elevator> aClass = elevator.getClass();
        Arrays.stream(aClass.getDeclaredFields()).forEach(field -> {
            try {
                field.setAccessible(true);
                Object o = field.get(elevator);
                if (o != null) {
                    String name = NameUtils.camel2Underline(field.getName());
                    if ("rescue_code".equalsIgnoreCase(name)) {
                        Integer fileValue = (Integer) o;
                        elevatorQueryWrapper.like(name, fileValue);
                    } else if ("city".equalsIgnoreCase(name) || "district".equalsIgnoreCase(name)) {
                        String fileValue = (String) o;
                        elevatorQueryWrapper.eq(name, fileValue);
                    } else {
                        String fileValue = (String) o;
                        elevatorQueryWrapper.like(name, fileValue);
                    }
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        });
        IPage<Elevator> page;
        if (StringUtils.isBlank(pageNum) || StringUtils.isBlank(pageSize)) {
            pageBean = new Page<>(0, Long.MAX_VALUE);
        } else {
            pageBean = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        }
        page = iElevatorService.page(pageBean, elevatorQueryWrapper);
        IPage<ElevatorDto> elevatorDtoIPage = BeanDtoVoUtils.elevatorIPageDto(page);
        return ResponseHelper.buildResponse(elevatorDtoIPage);
    }
}

