package org.springblade.modules.yjxt.business.resident.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.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.enums.DangerAreaResidentHouseHolderEnum;
import org.springblade.modules.yjxt.business.check.entity.DangerAreaCheckInfoEntity;
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.resident.vo.DangerAreaResidentInfoVO;
import org.springblade.modules.yjxt.business.transfer.entity.DamageTransferPersonInfoEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("galaxy-business/yj/resident")
@AllArgsConstructor
@Api(value = "应急系统危险区居民管理接口", tags = "增、删、改、查")
public class DangerAreaResidentController extends BladeController {

    private final IDangerAreaResidentService dangerAreaResidentService;


    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adCode", value = "行政区", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页")
    public R<IPage<DangerAreaResidentInfoVO>> page(@ApiIgnore DangerAreaResidentInfoDTO dto, Query query) {
        BladeUser user = AuthUtil.getUser();
        IPage<DangerAreaResidentInfoVO> pages = dangerAreaResidentService.selectPage(Condition.getPage(query), dto);
        return R.data(pages);
    }


    /**
     * 注册
     */
    @PostMapping("/record")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "")
    public R report(@RequestBody DangerAreaResidentInfoDTO dto) {
        if (Func.isNull(dto.getPersonId())) {
            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("此人员已注册");
        }
        dangerAreaResidentService.save(dto);
        if (!Func.isNull(dto.getIsHouseholder())){
            if (dto.getIsHouseholder().intValue() == DangerAreaResidentHouseHolderEnum.TYPE_YES.getCode().intValue()){
                //户主
                List<DangerAreaResidentInfoEntity>familyMembers = dto.getFamilyMembers();
                if (!Func.isNull(familyMembers)){
                    for (DangerAreaResidentInfoEntity residentInfoEntity:familyMembers){
                        residentInfoEntity.setDangerAreaCode(dto.getDangerAreaCode());
                        residentInfoEntity.setIsHouseholder(DangerAreaResidentHouseHolderEnum.TYPE_NO.getCode());
                        residentInfoEntity.setHouseholderId(dto.getPersonId());
                        dangerAreaResidentService.save(residentInfoEntity);
                    }
                }
            }
        }
        return R.status(true);
    }
}
