/*
 * Copyright (c) 2024 codvision.com All Rights Reserved.
 */

package com.codvision.basic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.auth.entity.excel.PersonExcel;
import com.codvision.auth.support.UserContext;
import com.codvision.base.support.EasyExcelUtil;
import com.codvision.basic.entity.Person;
import com.codvision.basic.entity.vo.PersonCountVO;
import com.codvision.basic.entity.vo.PersonPortraitVO;
import com.codvision.basic.entity.vo.PersonVO;
import com.codvision.basic.service.PersonService;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.annotation.ApiSecret;
import com.codvision.webcore.bean.QueryPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author KX
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/person")
@Api(tags = "人员信息")
public class PersonController {

    private final PersonService service;

    @PostMapping("/getPageResult")
    @ApiOperation("分页列表")
    public ResponseEntity<IPage<PersonVO>> getPageResult(@RequestBody QueryPage<Map<String, Object>> queryPage) {
        return new ResponseEntity<IPage<PersonVO>>().data(service.getPageResult(queryPage));
    }

    @PostMapping("/getPersonCount")
    @ApiOperation("根据地址Code统计人员数")
    public ResponseEntity<PersonCountVO> getPersonCount(@ApiParam(value = "地址Code", required = true) @RequestParam(value = "addressCode") String addressCode,
                                                        @ApiParam(value = "地址类型:ADDRESS_TYPE,1-楼幢;0-户室;4-小区/园区", required = true) @RequestParam(value = "addressType") String addressType) {
        return new ResponseEntity<PersonCountVO>().data(service.getPersonCount(addressCode, addressType));
    }

    @PostMapping("/getPersonListByAddressCode")
    @ApiOperation("根据地址编码查询人员信息")
    public ResponseEntity<IPage<PersonVO>> getPersonListByAddressCode(@RequestBody QueryPage<Map<String, Object>> queryPage) {
        return new ResponseEntity<IPage<PersonVO>>().data(service.getPersonListByAddressCode(queryPage));
    }

    @PostMapping("/details")
    @ApiOperation("详情")
    public ResponseEntity<PersonVO> details(@ApiParam(value = "主键id", required = true) @RequestParam(value = "id") Integer id) {
        return new ResponseEntity<PersonVO>().data(service.details(id));
    }

    @PostMapping("/add")
    @ApiOperation("添加")
    public ResponseEntity<String> add(@Validated @RequestBody Person person) {
        service.add(person);
        return new ResponseEntity<String>().ok();
    }

    @PostMapping("/update")
    @ApiOperation("编辑")
    public ResponseEntity<String> update(@RequestBody Person person) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (person.getId() == null) {
            throw new BusinessException(30002);
        }

        service.updateById(person);
        return responseEntity.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除人员信息")
    public ResponseEntity<String> del(@RequestBody List<Integer> personIds) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (CollectionUtils.isEmpty(personIds)) {
            throw new BusinessException(30002);
        }

        service.batchLogicDel(personIds);
        return responseEntity.ok();
    }

    @PostMapping("/getPersonPortrait")
    @ApiOperation("群体画像")
    public ResponseEntity<List<PersonPortraitVO>> getPersonPortrait(@RequestBody List<Integer> personIds) {
        ResponseEntity<List<PersonPortraitVO>> responseEntity = new ResponseEntity<>();
        if (CollectionUtils.isEmpty(personIds)) {
            throw new BusinessException(30002);
        }

        return responseEntity.data(service.getPersonPortrait(personIds));
    }

    @ApiSecret(ignore = true)
    @ApiOperation("下载人员信息导入模板")
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        EasyExcelUtil.downloadTemplate("人员信息模板表", "人员信息", PersonExcel.class, response);
    }

    @ApiSecret(ignore = true)
    @ApiOperation("人员信息导入")
    @PostMapping("/import")
    public ResponseEntity<String> excelImport(@RequestPart("file") MultipartFile file) {
        return new ResponseEntity<String>().data(service.excelImport(file));
    }

    @ApiSecret(ignore = true)
    @PostMapping("/export")
    @ApiOperation("人员信息导出")
    public ResponseEntity<String> export(@ApiParam(value = "关键字(姓名，手机号，证件号)") @RequestParam(required = false) String keyword,
                                         @ApiParam(value = "证件类型") @RequestParam(value = "idType", required = false) String idType,
                                         @ApiParam(value = "性别") @RequestParam(value = "sex", required = false) String sex,
                                         @ApiParam(value = "人口类型") @RequestParam(value = "personType", required = false) String personType,
                                         @ApiParam(value = "人员标签") @RequestParam(value = "personTags", required = false) String personTags,
                                         @ApiParam(value = "开始年龄") @RequestParam(value = "startAge", required = false) Integer startAge,
                                         @ApiParam(value = "结束年龄") @RequestParam(value = "endAge", required = false) Integer endAge,
                                         @ApiParam(value = "是否关联：0-否;1-是") @RequestParam(value = "flagRelated", required = false) String flagRelated,
                                         @ApiParam(value = "组织id") @RequestParam(value = "oid", required = false) Integer oid) {
        Map<String, Object> paramMap = new HashMap<>(16);
        if (StringUtils.isNotEmpty(keyword)) {
            paramMap.put("keyword", keyword);
        }
        if (StringUtils.isNotEmpty(idType)) {
            paramMap.put("idType", idType);
        }
        if (StringUtils.isNotEmpty(sex)) {
            paramMap.put("sex", sex);
        }
        if (StringUtils.isNotEmpty(personType)) {
            paramMap.put("personType", personType);
        }
        if (StringUtils.isNotEmpty(personTags)) {
            paramMap.put("personTags", personTags);
        }
        if (startAge != null) {
            paramMap.put("startAge", startAge);
        }
        if (endAge != null) {
            paramMap.put("endAge", endAge);
        }
        if (StringUtils.isNotEmpty(flagRelated)) {
            paramMap.put("flagRelated", flagRelated);
        }
        if (oid != null) {
            paramMap.put("oid", oid);
        } else {
            paramMap.put("oid", UserContext.get().getActiveOid());
        }
        service.excelExport(paramMap);
        return new ResponseEntity<String>().ok();
    }

}
