package com.changhu.module.management.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changhu.common.annotation.CheckUserType;
import com.changhu.common.annotation.JsonBody;
import com.changhu.common.db.enums.UserType;
import com.changhu.common.exception.MessageException;
import com.changhu.common.pojo.vo.SelectNodeVo;
import com.changhu.module.management.pojo.entity.EnterprisesUnit;
import com.changhu.module.management.pojo.params.EnterprisesUnitSaveOrUpdateParams;
import com.changhu.module.management.pojo.queryParams.EnterprisesUnitPagerQueryParams;
import com.changhu.module.management.pojo.vo.EnterprisesUnitPagerVo;
import com.changhu.module.management.service.EnterprisesUnitService;
import com.changhu.support.mybatisplus.pojo.params.PageParams;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 20252
 * @createTime 2024/9/3 上午10:17
 * @desc EnterprisesUnitController...
 */
@Tag(name = "企事业单位")
@JsonBody
@RequestMapping("/enterprisesUnit")
public class EnterprisesUnitController {

    @Autowired
    private EnterprisesUnitService enterprisesUnitService;

    @Operation(summary = "分页查询")
    @PostMapping("/pager")
    @CheckUserType(userTypes = {UserType.MANAGEMENT_POLICE, UserType.MANAGEMENT_SUPER})
    public Page<EnterprisesUnitPagerVo> pager(@RequestBody PageParams<EnterprisesUnitPagerQueryParams, EnterprisesUnitPagerVo> queryParams) {
        return enterprisesUnitService.pager(queryParams);
    }

    @Operation(summary = "新增或保存")
    @PostMapping("/saveOrUpdate")
    @CheckUserType(userTypes = {UserType.MANAGEMENT_POLICE, UserType.MANAGEMENT_SUPER})
    public void saveOrUpdate(@RequestBody @Valid EnterprisesUnitSaveOrUpdateParams params) {
        enterprisesUnitService.saveOrUpdate(params);
    }

    @Operation(summary = "根据id删除")
    @DeleteMapping("/deleteById")
    @CheckUserType(userTypes = {UserType.MANAGEMENT_POLICE, UserType.MANAGEMENT_SUPER})
    public void deleteById(@RequestParam @Schema(description = "企事业单位id") Long enterprisesUnitId) {
        boolean b = enterprisesUnitService.removeById(enterprisesUnitId);
        if (!b) {
            throw new MessageException();
        }
    }

    @Operation(summary = "根据行政区划编码查询下面的企事业单位")
    @PostMapping("/queryListByAdministrativeDivisionCodes")
    public List<SelectNodeVo<Long>> queryListByAdministrativeDivisionCodes(@RequestBody @Schema(description = "行政区划编码") List<String> administrativeDivisionCodes) {
        return enterprisesUnitService.queryListByAdministrativeDivisionCodes(administrativeDivisionCodes);
    }

    @Operation(summary = "企事业单位地图点位")
    @GetMapping("/mapPoint")
    public List<EnterprisesUnit> mapPoint() {
        return enterprisesUnitService.list();
    }

}
