/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/28
 */
package org.springblade.modules.business.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.business.data.dto.RtuDataGroundStoreDTO;
import org.springblade.modules.business.data.dto.RtuDataRainDTO;
import org.springblade.modules.business.data.dto.RtuDataRainStoreDTO;
import org.springblade.modules.business.data.dto.RtuDataRiverDTO;
import org.springblade.modules.business.data.entity.RtuDataGroundStoreEntity;
import org.springblade.modules.business.data.entity.RtuDataRainEntity;
import org.springblade.modules.business.data.entity.RtuDataRainStoreEntity;
import org.springblade.modules.business.data.entity.RtuDataRiverStoreEntity;
import org.springblade.modules.business.data.service.IRtuDataRainHistoreService;
import org.springblade.modules.business.data.service.IRtuDataRainService;
import org.springblade.modules.business.data.vo.RtuDataGroundStoreVO;
import org.springblade.modules.business.data.vo.RtuDataRainStoreVO;
import org.springblade.modules.business.data.vo.RtuDataRainVO;
import org.springblade.modules.business.data.wrapper.RtuDataGroundHistoreWrapper;
import org.springblade.modules.business.data.wrapper.RtuDataRainHistoreWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


/***
 * Date:2022/8/28
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/rtu/data/rain")
@AllArgsConstructor
@Api(value = "降水实时数据管理", tags = "测站实时数据管理")
public class RtuDataRainController extends BladeController {
    private final IRtuDataRainService dataRainService;
    private final IRtuDataRainHistoreService historeService;
    private final IDeptRegionService deptRegionService;

    /**
     * 分页
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "deptId", value = "机构部门ID", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页")
    public R<IPage<RtuDataRainVO>> page(@ApiIgnore RtuDataRainDTO rtuDataRainDTO, Query query) {
        BladeUser user = AuthUtil.getUser();
        if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
//            if (rtuDataRainDTO.getAdCode() == null) {
//                rtuDataRainDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
//            }
        } else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
//            if (rtuDataRainDTO.getAdCode() == null) {
//                LambdaQueryWrapper<DeptRegionEntity> deptRegionEntityLambdaQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
//                deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getIsDeleted, 0);
//                deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
//                DeptRegionEntity deptRegionEntity = deptRegionService.getOne(deptRegionEntityLambdaQueryWrapper);
//                rtuDataRainDTO.setAdCode(deptRegionEntity.getAdcd());
//            }

            if (rtuDataRainDTO.getDeptId() == null) {
                rtuDataRainDTO.setDeptId(Func.toLong(user.getDeptId()));
            }
        }
        IPage<RtuDataRainVO> pages = dataRainService.selectPage(Condition.getPage(query), rtuDataRainDTO);
        return R.data(pages);
    }


    /**
     * 分页
     */
    @GetMapping("/history/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页")
    public R<IPage<RtuDataRainStoreVO>> historyPage(@ApiIgnore RtuDataRainStoreDTO dto, Query query) {
        IPage<RtuDataRainStoreEntity> pages = historeService.selectPage(Condition.getPage(query), dto);
        return R.data(RtuDataRainHistoreWrapper.build().pageVO(pages));
    }

    /**
     * 图表-线
     */
    @GetMapping("/line")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页")
    public R<List<RtuDataRainStoreEntity>> line(@ApiIgnore RtuDataRainDTO dto) {
        Calendar calendar = Calendar.getInstance();
        Date endTm = calendar.getTime();
        calendar.add(Calendar.HOUR_OF_DAY, -24);
        Date startTm = calendar.getTime();
        LambdaQueryWrapper<RtuDataRainStoreEntity> queryWrapper = Wrappers.<RtuDataRainStoreEntity>query().lambda();
        queryWrapper.eq(RtuDataRainStoreEntity::getIsDeleted, 0);
        queryWrapper.eq(RtuDataRainStoreEntity::getRtuCode, dto.getRtuCode());
        queryWrapper.ge(RtuDataRainStoreEntity::getTm, startTm);
        queryWrapper.lt(RtuDataRainStoreEntity::getTm, endTm);
        List<RtuDataRainStoreEntity> list = historeService.list(queryWrapper);
        return R.data(list);
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入 RtuDataRainEntity")
    public R<RtuDataRainEntity> detail(RtuDataRainEntity entity) {
        LambdaQueryWrapper<RtuDataRainEntity> wrapper = Wrappers.<RtuDataRainEntity>query().lambda();
        wrapper.eq(RtuDataRainEntity::getIsDeleted, 0L);
        wrapper.eq(RtuDataRainEntity::getRtuCode, entity.getRtuCode());
        RtuDataRainEntity rainEntity = dataRainService.getOne(wrapper);
        return R.data(rainEntity);
    }
}
