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

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.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.enums.DangerAreaResidentHouseHolderEnum;
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.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/***
 * Date:2023/11/10
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("yjxt-business/public/rest")
@AllArgsConstructor
@Api(value = "应急系统接口", tags = "提供网页填报接口")
public class YjxtPublicDataController extends BladeController {

    private final IDamageTransferPersonService damageTransferPersonService;

    private final IDangerAreaResidentService dangerAreaResidentService;


    @PostMapping("/transfer/resident/register")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "")
    public R residentRegister(@RequestBody DamageTransferPersonInfoDTO dto) {
        if (Func.isNull(dto.getWarnId()) || Func.isNull(dto.getPersonId()) || Func.isNull(dto.getPersonName())) {
            return R.fail("参数错误");
        }
        if (Func.isNull(dto.getFamilyMembers())) {
            return R.fail("操作失败");
        }
        for (String id : dto.getFamilyMembers()) {
            DangerAreaResidentInfoEntity entity = dangerAreaResidentService.getById(Func.toLong(id));
            if (Func.notNull(entity)) {
                LambdaQueryWrapper<DamageTransferPersonInfoEntity> wrapper = Wrappers.<DamageTransferPersonInfoEntity>query().lambda();
                wrapper.eq(DamageTransferPersonInfoEntity::getWarnId, dto.getWarnId());
                wrapper.eq(DamageTransferPersonInfoEntity::getPersonId, entity.getPersonId());
                wrapper.eq(DamageTransferPersonInfoEntity::getPersonName, entity.getPersonName());
                wrapper.last("limit 1");
                DamageTransferPersonInfoEntity detail = damageTransferPersonService.getOne(wrapper);
                if (Func.notNull(detail)) {
                    detail.setRemark(dto.getRemark());
                    detail.setKeepPhotos(dto.getKeepPhotos());
                    damageTransferPersonService.updateById(detail);
                } else {
                    detail = new DamageTransferPersonInfoEntity();
                    detail.setWarnId(dto.getWarnId());
                    detail.setPersonId(entity.getPersonId());
                    detail.setPersonName(entity.getPersonName());
                    detail.setRemark(dto.getRemark());
                    detail.setKeepPhotos(dto.getKeepPhotos());
                    damageTransferPersonService.save(detail);
                }
            }
        }

//        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);
    }

    /**
     * 群众注册
     */
    @PostMapping("/dangerarea/resident/register")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "")
    public R register(@RequestBody DangerAreaResidentInfoDTO dto) {
        if (Func.isNull(dto.getPersonId()) || Func.isNull(dto.getPersonName())) {
            return R.fail("参数错误");
        }
        LambdaQueryWrapper<DangerAreaResidentInfoEntity> wrapper = Wrappers.<DangerAreaResidentInfoEntity>query().lambda();
        wrapper.eq(DangerAreaResidentInfoEntity::getIsDeleted, 0);
        wrapper.eq(DangerAreaResidentInfoEntity::getPersonId, dto.getPersonId());
        long count = dangerAreaResidentService.count(wrapper);
        if (count > 0) {
            return R.fail("此人员已注册");
        }
        dto.setRegisterType(2);
        dangerAreaResidentService.save(dto);
        if (!Func.isNull(dto.getIsHouseholder())) {
            if (dto.getIsHouseholder().intValue() == DangerAreaResidentHouseHolderEnum.TYPE_YES.getCode().intValue()) {
                //户主
                List<DangerAreaResidentInfoEntity> familyMembers = dto.getFamilyMembers();
                if (Func.notNull(familyMembers)) {
                    for (DangerAreaResidentInfoEntity residentInfoEntity : familyMembers) {
                        residentInfoEntity.setDangerAreaPid(dto.getDangerAreaPid());
                        residentInfoEntity.setIsHouseholder(DangerAreaResidentHouseHolderEnum.TYPE_NO.getCode());
                        residentInfoEntity.setHouseholderId(dto.getPersonId());
                        residentInfoEntity.setRegisterType(2);
                        dangerAreaResidentService.save(residentInfoEntity);
                    }
                }
            }
        }
        return R.status(true);
    }


    @GetMapping("/resident/family/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "personId", value = "户主身份证号", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页")
    public R<List<DangerAreaResidentInfoEntity>> getFamilyMembers(@ApiIgnore DangerAreaResidentInfoDTO dto) {
        if (Func.isNull(dto.getPersonId())) {
            return R.fail("参数错误");
        }
        LambdaQueryWrapper<DangerAreaResidentInfoEntity> wrapper = Wrappers.<DangerAreaResidentInfoEntity>query().lambda();
        wrapper.eq(DangerAreaResidentInfoEntity::getPersonId, dto.getPersonId());
        if(Func.notNull(dto.getPersonName())){
            wrapper.eq(DangerAreaResidentInfoEntity::getPersonName,dto.getPersonName());
        }
        wrapper.last("limit 1");
        DangerAreaResidentInfoEntity entity=dangerAreaResidentService.getOne(wrapper);
        if (Func.isNull(entity)){
            return R.data(new ArrayList<>(0));
        }
        if (0 ==  entity.getIsHouseholder()){
            dto.setPersonName(null);
            dto.setPersonId(entity.getHouseholderId());
        }
        List<DangerAreaResidentInfoEntity> list = dangerAreaResidentService.getFamilyMembers(dto);
        return R.data(list);
    }

}
