package com.yuchen.saas.park.service.controller.basic;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.park.PmgBatParkBuildingInfo;
import com.yuchen.saas.park.api.entity.park.PmgBatParkFileInfo;
import com.yuchen.saas.park.api.entity.park.PmgBatParkFileRelevance;
import com.yuchen.saas.park.api.vo.park.PmgBatParkBuildingInfoVo;
import com.yuchen.saas.park.api.vo.park.PmgBatParkSearchVo;
import com.yuchen.saas.park.service.service.PmgBatParkBuildingInfoService;
import com.yuchen.saas.park.service.service.PmgBatParkFileInfoService;
import com.yuchen.saas.park.service.service.PmgBatParkFileRelevanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author 刘碧清
 * @date 2021/04/26 10:16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/parkBuildingInfo")
@Api(value = "楼宇信息对象", tags = "楼宇信息接口")
public class PmgBatParkBuildingInfoController {
    @Autowired
    private PmgBatParkBuildingInfoService pmgBatParkBuildingInfoService;

    @Autowired
    private PmgBatParkFileRelevanceService pmgBatParkFileRelevanceService;

    @Autowired
    private PmgBatParkFileInfoService pmgBatParkFileInfoService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入楼宇信息")
    public R<PmgBatParkBuildingInfo> detail(PmgBatParkBuildingInfo pmgBatParkBuildingInfo) {
        PmgBatParkBuildingInfo detail = pmgBatParkBuildingInfoService.getOne(Condition.getQueryWrapper(pmgBatParkBuildingInfo));
        return R.data(detail);
    }

    /**
     * 获取所有楼栋数据
     */
    @GetMapping("/getAll")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "全部数据", notes = "传入楼宇信息")
    public R<List<PmgBatParkBuildingInfo>> getAll(String parkId) {
        return R.data(pmgBatParkBuildingInfoService.getAll(parkId));
    }

    /**
     * 新增楼宇数据
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增楼宇数据", notes = "传入楼宇管理信息")
    public R submit(@Valid @RequestBody PmgBatParkBuildingInfoVo pmgBatParkBuildingInfoEntity) {
        return pmgBatParkBuildingInfoService.submit(pmgBatParkBuildingInfoEntity);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改", notes = "传入楼宇管理信息")
    public R update(@Valid @RequestBody PmgBatParkBuildingInfo pmgBatParkBuildingInfo) {
        // 判断楼宇名字和唯一标识是否重复
        LambdaQueryWrapper<PmgBatParkBuildingInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmgBatParkBuildingInfo::getParkId,pmgBatParkBuildingInfo.getParkId());
        wrapper.ne(PmgBatParkBuildingInfo::getId,pmgBatParkBuildingInfo.getId());
        wrapper.nested(qw -> qw.eq(PmgBatParkBuildingInfo::getBMark,pmgBatParkBuildingInfo.getBMark()).or().eq(PmgBatParkBuildingInfo::getBuildingName,
                pmgBatParkBuildingInfo.getBuildingName()));
        if (pmgBatParkBuildingInfoService.count(wrapper) > 0){
            return R.error("楼栋名称或楼栋标识重复！");
        }
        if(!Func.isEmpty(pmgBatParkBuildingInfo.getFileUrl())){

            LambdaQueryWrapper<PmgBatParkFileInfo> fileInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            fileInfoLambdaQueryWrapper.eq(PmgBatParkFileInfo::getFileSrc,pmgBatParkBuildingInfo.getFileUrl());
            PmgBatParkFileInfo fileInfo = pmgBatParkFileInfoService.getOne(fileInfoLambdaQueryWrapper);

            //根据楼栋id查询关联表文件id并更新
            LambdaUpdateWrapper<PmgBatParkFileRelevance> queryWrapper = new LambdaUpdateWrapper<>();
            queryWrapper.eq(PmgBatParkFileRelevance::getRelevanceId,pmgBatParkBuildingInfo.getId());
            queryWrapper.set(PmgBatParkFileRelevance::getFileId,fileInfo.getId());
            pmgBatParkFileRelevanceService.update(queryWrapper);
        }
        return R.status(pmgBatParkBuildingInfoService.updateById(pmgBatParkBuildingInfo));
    }

    @DeleteMapping("/remove")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(pmgBatParkBuildingInfoService.deleteData(ids));
    }

    /**
     * 高级搜索
     */
    @GetMapping("/getRoomAll")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "全部数据", notes = "传入楼宇信息")
    public R<List<Map<String, Object>>> getRoomAll(PmgBatParkSearchVo pmgBatParkSearch) {
        List<Map<String, Object>> storeyList = pmgBatParkBuildingInfoService.getRoomAll(pmgBatParkSearch);
        return R.data(storeyList);
    }

    /**
     * 通过园区ID获取楼栋列表
     *
     * @param parkId 园区ID
     * @return
     */
    @GetMapping("/getBuildingListByParkId")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "全部数据", notes = "传入楼宇信息")
    public R<List<PmgBatParkBuildingInfo>> getBuildingListByParkId(String parkId) {
        LambdaQueryWrapper<PmgBatParkBuildingInfo> carWrapper = Wrappers.lambdaQuery();
        return R.data(pmgBatParkBuildingInfoService.list(carWrapper.eq(PmgBatParkBuildingInfo::getParkId, parkId)));
    }

    /**
     * 通过园区id获取楼栋信息
     *
     * @param parkId
     * @return
     */
    @GetMapping("/getByParkId")
    @ApiOperation(value = "通过园区id获取楼栋", notes = "通过园区id获取楼栋")
    public R<List<PmgBatParkBuildingInfo>> getByParkId(@RequestParam Long parkId) {
        List<PmgBatParkBuildingInfo> list = pmgBatParkBuildingInfoService.list(Wrappers.<PmgBatParkBuildingInfo>lambdaQuery().
                eq(PmgBatParkBuildingInfo::getParkId, parkId).orderByDesc(PmgBatParkBuildingInfo::getCreateTime));
        return R.data(list);
    }

}
