/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/11/10
 */
package org.springblade.modules.yjxt.business.transfer.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.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.yjxt.business.resident.dto.DangerAreaResidentInfoDTO;
import org.springblade.modules.yjxt.business.resident.entity.DangerAreaResidentInfoEntity;
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.transfer.vo.DamageTransferPersonInfoVO;
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/10
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/yj/transfer")
@AllArgsConstructor
@Api(value = "应急系统灾害转移管理接口", tags = "增、删、改、查")
public class DamageTransferController extends BladeController {

    private final IDamageTransferPersonService damageTransferPersonService;

    private final IDangerAreaResidentService dangerAreaResidentService;

    private final IOriginalWarningService originalWarningService;


    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warnId", value = "预警id", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页")
    public R<IPage<DamageTransferPersonInfoVO>> page(@ApiIgnore DamageTransferPersonInfoDTO dto, Query query) {
        if (Func.isNull(dto.getWarnId())){
            return R.fail("参数错误");
        }
        //BladeUser user = AuthUtil.getUser();
        LambdaQueryWrapper<OriginalWarningInfoEntity> wrapper = Wrappers.<OriginalWarningInfoEntity>query().lambda();
        wrapper.eq(OriginalWarningInfoEntity::getIsDeleted, 0);
        wrapper.eq(OriginalWarningInfoEntity::getWarnId, dto.getWarnId());
        OriginalWarningInfoEntity detail = originalWarningService.getOne(wrapper);
        if (Func.isNull(detail)){
            return R.fail("系统错误");
        }
        dto.setDangerAreaCode(detail.getWarnAdcd());
        IPage<DamageTransferPersonInfoVO> pages = damageTransferPersonService.selectPage(Condition.getPage(query), dto);
        return R.data(pages);
    }


    /**
     * 转移人员记录
     */
    @PostMapping("/person/record")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "")
    public R record(@RequestBody DamageTransferPersonInfoDTO dto) {
        if (Func.isNull(dto.getWarnId()) || Func.isNull(dto.getPersonId()) || Func.isNull(dto.getPersonName())) {
            return R.fail("参数错误");
        }
        LambdaQueryWrapper<DamageTransferPersonInfoEntity> wrapper = Wrappers.<DamageTransferPersonInfoEntity>query().lambda();
        wrapper.eq(DamageTransferPersonInfoEntity::getIsDeleted, 0);
        wrapper.eq(DamageTransferPersonInfoEntity::getWarnId, dto.getWarnId());
        wrapper.eq(DamageTransferPersonInfoEntity::getPersonId, dto.getPersonId());
        long count = damageTransferPersonService.count(wrapper);
        if (count > 0) {
            return R.fail("该人员已经登记");
        }
        damageTransferPersonService.save(dto);
        if (Func.notNull(dto.getFamilyMembersTransferStatus())) {
            if (dto.getFamilyMembersTransferStatus() == 1) {
                DangerAreaResidentInfoDTO residentInfoDTO = new DangerAreaResidentInfoDTO();
                residentInfoDTO.setPersonId(dto.getPersonId());
                List<DangerAreaResidentInfoEntity> residentInfoEntityList = dangerAreaResidentService.getFamilyMembers(residentInfoDTO);
                if (Func.notNull(residentInfoEntityList)) {
                    for (DangerAreaResidentInfoEntity entity : residentInfoEntityList) {
                        if (!dto.getPersonId().equals(entity.getPersonId())) {
                            wrapper = Wrappers.<DamageTransferPersonInfoEntity>query().lambda();
                            wrapper.eq(DamageTransferPersonInfoEntity::getIsDeleted, 0);
                            wrapper.eq(DamageTransferPersonInfoEntity::getWarnId, dto.getWarnId());
                            wrapper.eq(DamageTransferPersonInfoEntity::getPersonId, entity.getPersonId());
                            DamageTransferPersonInfoEntity detail = damageTransferPersonService.getOne(wrapper);
                            if (Func.isNull(detail)) {
                                detail = new DamageTransferPersonInfoEntity();
                            }
                            detail.setWarnId(dto.getWarnId());
                            detail.setPersonId(entity.getPersonId());
                            detail.setPersonName(entity.getPersonName());
                            detail.setRemark(dto.getRemark());
                            damageTransferPersonService.saveOrUpdate(detail);
                        }
                    }
                }
            }
        }
        return R.status(true);
    }
}
