/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/11/10
 */
package org.springblade.share;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
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.resident.dto.DangerAreaResidentInfoDTO;
import org.springblade.modules.yjxt.business.resident.service.IDangerAreaResidentService;
import org.springblade.modules.yjxt.business.transfer.dto.DamageTransferPersonInfoDTO;
import org.springblade.modules.yjxt.business.transfer.entity.DamageTransferPersonInfoEntity;
import org.springblade.modules.yjxt.business.transfer.service.IDamageTransferPersonService;
import org.springblade.modules.yjxt.business.warn.entity.OriginalWarningInfoEntity;
import org.springblade.modules.yjxt.business.warn.service.IOriginalWarningService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/***
 * Date:2023/11/10
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("ywxt-business/public/data/share")
@AllArgsConstructor
@Api(value = "共享管理接口", tags = "提供第三方查询接口")
public class DataShareController extends BladeController {

    private final IUserService userService;
    private final IDangerAreaCheckService danagerAreaCheckService;
    private final IDamageTransferPersonService damageTransferPersonService;
    private final IDangerAreaResidentService dangerAreaResidentService;
    private final IOriginalWarningService warningService;

    /**
     * 巡查巡检列表查询，不分页
     */
    @GetMapping("/yj/check/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warnId", value = "预警ID", paramType = "query", dataType = "string")
    })
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "不分页", notes = "")
    public R<List<JSONObject>> yjCheckList(DangerAreaCheckInfoDTO dto) {
        if (Func.isNull(dto.getWarnId())) {
            return R.fail("参数错误");
        }
        if (dto.getWarnId().length() ==0){
            return R.fail("参数错误");
        }
        LambdaQueryWrapper<DangerAreaCheckInfoEntity> wrapper = Wrappers.<DangerAreaCheckInfoEntity>query().lambda();
        wrapper.eq(DangerAreaCheckInfoEntity::getWarnId, dto.getWarnId());
        wrapper.orderByAsc(DangerAreaCheckInfoEntity::getCreateTime);
        List<DangerAreaCheckInfoEntity> list = danagerAreaCheckService.list(wrapper);
        List<JSONObject> datas = new ArrayList<>(list.size());
        for (DangerAreaCheckInfoEntity entity : list) {
            JSONObject d = new JSONObject();
            d.put("warnId", entity.getWarnId());
            User user = userService.getById(entity.getCreateUser());
            if (!Func.isNull(user)) {
                d.put("checkPerson", user.getRealName());
            } else {
                d.put("checkPerson", "");
            }
            d.put("checkTime", entity.getCreateTime().getTime());
            if (!Func.isNull(entity.getLongitude())) {
                d.put("lng", entity.getLongitude());
            }
            if (!Func.isNull(entity.getLatitude())) {
                d.put("lat", entity.getLatitude());
            }
            if (!Func.isNull(entity.getAddress())) {
                d.put("location", entity.getAddress());
            }
            d.put("isFlood", entity.getIsFlood());
            d.put("isDamage", entity.getIsDamage());
            if (!Func.isNull(entity.getRemark())) {
                d.put("remark", entity.getRemark());
            }
            JSONArray photos = new JSONArray();
            if (!Func.isNull(entity.getCheckPhotos())) {
                String photo = entity.getCheckPhotos();
                String[] photoArr = photo.trim().split(",");
                for (String url : photoArr) {
                    JSONObject p = new JSONObject();
                    p.put("url", BusinessConstant.OSS_BASE_PATH + url);
                    photos.add(p);
                }
            }
            d.put("photos", photos);
            datas.add(d);
        }
        return R.data(datas);
    }

    /**
     * 灾害转移统计查询
     */
    @GetMapping("/yj/transfer/count")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warnId", value = "预警ID", paramType = "query", dataType = "string")
    })
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "不分页", notes = "")
    public R<List<JSONObject>> yjTransferCount(DamageTransferPersonInfoDTO dto) {
        if (Func.isNull(dto.getWarnId())) {
            return R.fail("参数错误");
        }
        LambdaQueryWrapper<OriginalWarningInfoEntity> wrapper = Wrappers.<OriginalWarningInfoEntity>query().lambda();
        wrapper.eq(OriginalWarningInfoEntity::getWarnId, dto.getWarnId());
        OriginalWarningInfoEntity warningInfoEntity = warningService.getOne(wrapper);
        if (Func.isNull(warningInfoEntity)){
            return R.fail("预警数据空");
        }

        List<JSONObject> datas = new ArrayList<>(1);

        JSONObject vo = new JSONObject();
        DangerAreaResidentInfoDTO residentInfoDTO=new DangerAreaResidentInfoDTO();
        residentInfoDTO.setDangerAreaCode(warningInfoEntity.getWarnAdcd());
        long totalTransferPerson =dangerAreaResidentService.residentCount(residentInfoDTO);

        DamageTransferPersonInfoDTO transferPersonInfoDTO = new DamageTransferPersonInfoDTO();
        transferPersonInfoDTO.setWarnId(dto.getWarnId());
        long safetyTransferPerson = damageTransferPersonService.transferPersonCount(transferPersonInfoDTO);

        vo.put("totalTransferPerson",totalTransferPerson);
        vo.put("safetyTransferPerson",safetyTransferPerson);

        JSONArray personList = new JSONArray();

        LambdaQueryWrapper<DamageTransferPersonInfoEntity> wrapper2 = Wrappers.<DamageTransferPersonInfoEntity>query().lambda();
        wrapper2.eq(DamageTransferPersonInfoEntity::getIsDeleted, 0);
        wrapper2.eq(DamageTransferPersonInfoEntity::getWarnId, dto.getWarnId());
        List<DamageTransferPersonInfoEntity> transferPersonInfoEntityList = damageTransferPersonService.list(wrapper2);
        if (!Func.isNull(transferPersonInfoEntityList)){
            for (DamageTransferPersonInfoEntity transferPersonInfoEntity:transferPersonInfoEntityList){
                JSONObject person = new JSONObject();
                person.put("personName",transferPersonInfoEntity.getPersonName());
                person.put("recordTime",transferPersonInfoEntity.getCreateTime().getTime());
                personList.add(person);
            }
        }

        vo.put("transferPersonList",personList);

        datas.add(vo);

        return R.data(datas);
    }
}
