/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/11/9
 */
package org.springblade.modules.yjxt.business.check.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.enums.DanagerAreaCehckTypeEnum;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.yjxt.business.check.dto.DangerAreaCheckInfoDTO;
import org.springblade.modules.yjxt.business.check.entity.DangerAreaCheckInfoEntity;
import org.springblade.modules.yjxt.business.check.service.IDangerAreaCheckService;
import org.springblade.modules.yjxt.business.check.vo.DangerAreaCheckInfoVO;
import org.springblade.modules.yjxt.business.check.wrapper.DanagerAreaCheckWrapper;
import org.springblade.modules.yjxt.business.warn.entity.OriginalWarningInfoEntity;
import org.springblade.modules.yjxt.business.warn.service.IOriginalWarningService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/***
 * Date:2023/11/9
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/yj/check")
@AllArgsConstructor
@Api(value = "应急系统巡查巡检管理接口", tags = "增、删、改、查")
public class DangerAreaCheckController extends BladeController {

    private final IDangerAreaCheckService danagerAreaCheckService;
    private final IOriginalWarningService originalWarningService;
    private final IDeptRegionService deptRegionService;

    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "名称", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页")
    public R<IPage<DangerAreaCheckInfoVO>> page(@ApiIgnore DangerAreaCheckInfoDTO dto, Query query) {
        BladeUser user = AuthUtil.getUser();
        if (Func.isNull(dto.getAdcd())) {
            if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
                dto.setAdcd(BusinessConstant.REGION_NM_ADCODE);
            } else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName())) {
                LambdaQueryWrapper<DeptRegionEntity> wrapper = Wrappers.<DeptRegionEntity>query().lambda();
                wrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
                wrapper.last("limit 1");
                DeptRegionEntity deptRegionEntity = deptRegionService.getOne(wrapper);
                if (Func.isNull(deptRegionEntity)) {
                    return R.fail("查询失败");
                }
                dto.setAdcd(deptRegionEntity.getAdcd());
            } else if (BusinessConstant.ROLE_YJ_SERVICE_PERSON.equals(user.getRoleName())) {
                LambdaQueryWrapper<DeptRegionEntity> wrapper = Wrappers.<DeptRegionEntity>query().lambda();
                wrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
                wrapper.last("limit 1");
                DeptRegionEntity deptRegionEntity = deptRegionService.getOne(wrapper);
                if (Func.isNull(deptRegionEntity)) {
                    return R.fail("查询失败");
                }
                dto.setAdcd(deptRegionEntity.getAdcd());
            } else if (BusinessConstant.ROLE_QI_ADMIN.equals(user.getRoleName())) {
                LambdaQueryWrapper<DeptRegionEntity> wrapper = Wrappers.<DeptRegionEntity>query().lambda();
                wrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
                wrapper.last("limit 1");
                DeptRegionEntity deptRegionEntity = deptRegionService.getOne(wrapper);
                if (Func.isNull(deptRegionEntity)) {
                    return R.fail("查询失败");
                }
                dto.setAdcd(deptRegionEntity.getAdcd());
            } else {
                return R.fail("查询失败");
            }
        }
        IPage<DangerAreaCheckInfoVO> pages = danagerAreaCheckService.selectPage(Condition.getPage(query), dto);
        List<DangerAreaCheckInfoVO> list = pages.getRecords();
        for (DangerAreaCheckInfoVO vo : list) {
            DanagerAreaCheckWrapper.build().entityVO(vo);
        }
        return R.data(pages);
    }


    /**
     * 上报
     */
    @PostMapping("/report")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入 User")
    public R report(@RequestBody DangerAreaCheckInfoEntity entity) {
        if (Func.isNull(entity.getAdcd())){
            return R.fail("参数错误");
        }
        if (Func.isNull(entity.getCheckType())) {
            return R.fail("参数错误");
        }
        if (DanagerAreaCehckTypeEnum.CHECK_2.getCode().intValue() == entity.getCheckType().intValue()) {
            if (Func.isNull(entity.getWarnId())) {
                return R.fail("参数错误");
            }
        }
//        if (Func.notNull(entity.getWarnId())) {
//            LambdaQueryWrapper<OriginalWarningInfoEntity> wrapper = Wrappers.<OriginalWarningInfoEntity>query().lambda();
//            wrapper.eq(OriginalWarningInfoEntity::getWarnId, entity.getWarnId());
//            wrapper.last("limit 1");
//            OriginalWarningInfoEntity warningInfoEntity = originalWarningService.getOne(wrapper);
//            if (Func.notNull(warningInfoEntity)) {
//                entity.setAdcd(warningInfoEntity.getWarnAdcd());
//            }
//        }
        danagerAreaCheckService.save(entity);
        return R.status(true);
    }


    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "")
    public R<DangerAreaCheckInfoVO> detail(DangerAreaCheckInfoDTO entity) {
        if (entity.getId() == null) {
            return R.fail("参数错误");
        }
        DangerAreaCheckInfoEntity detail = danagerAreaCheckService.getById(entity.getId());
        return R.data(DanagerAreaCheckWrapper.build().entityVO(detail));
    }
}
