package com.njcn.advance.controller.assess;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.advance.pojo.param.assess.AssessUserQuery;
import com.njcn.advance.pojo.po.assess.*;
import com.njcn.advance.service.assess.*;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.njcn.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cdf
 * @since 2025-06-06
 */
@RestController
@RequestMapping("/assessUser")
@Api(tags = "评估用户管理")
@RequiredArgsConstructor
public class AssessUserController extends BaseController {

    private final IAssessUserService assessUserService;
    private final IAssessTransformerParamService transformerParamService;
    private final IAssessCapacitorParamService capacitorParamService;
    private final IAssessWindLoadParamService windLoadParamService;
    private final IAssessLoadParamService loadParamService;
    private final IAssessShockLoadParamService shockLoadParamService;


    @ApiOperation("获取评估用户列表")
    @PostMapping("/list")
    public HttpResult<List<AssessUser>> list(@RequestBody AssessUserQuery assessUserQuery) {
        LambdaQueryWrapper<AssessUser> wrapper = Wrappers.<AssessUser>lambdaQuery()
                .like(assessUserQuery.getDeptName() != null, AssessUser::getDeptName, assessUserQuery.getDeptName())
                .like(assessUserQuery.getUserScale() != null, AssessUser::getUserScale, assessUserQuery.getUserScale());
        List<AssessUser> list = assessUserService.list(wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), list, CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("分页查询评估用户")
    @PostMapping("/page")
    public HttpResult<Page<AssessUser>> page(@RequestBody AssessUserQuery assessUserQuery) {
        Page<AssessUser> pageData = assessUserService.page(assessUserQuery);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), pageData, CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("根据ID获取评估用户详情")
    @ApiImplicitParam(name = "assessId", value = "评估用户ID", required = true, type = "String")
    @GetMapping("/getInfo")
    public HttpResult<AssessUser> getInfo(@RequestParam("assessId") String assessId) {
        AssessUser assessUser = assessUserService.getByAssessId(assessId);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), assessUser, CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("新增评估用户")
    @PostMapping("/add")
    public HttpResult<String> add(@RequestBody @Validated AssessUserQuery assessUser) {
        String result = assessUserService.add(assessUser);
        if (StrUtil.isNotBlank(result)) {
            return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), result, CommonResponseEnum.SUCCESS.getMessage());
        } else {
            return HttpResultUtil.assembleResult(CommonResponseEnum.FAIL.getCode(), null, CommonResponseEnum.FAIL.getMessage());
        }
    }

    @ApiOperation("修改评估用户")
    @PostMapping("/edit")
    public HttpResult<Boolean> edit(@RequestBody @Validated AssessUserQuery.UpdateAssessUserQuery updateAssessUserQuery) {
        assessUserService.update(updateAssessUserQuery);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), true, CommonResponseEnum.SUCCESS.getMessage());
    }


    @ApiOperation("批量删除评估用户")
    @ApiImplicitParam(name = "assessIds", value = "评估用户ID列表，用逗号分隔", required = true, type = "String")
    @PostMapping("/batchDel")
    public HttpResult<Boolean> batchDelete(@RequestBody List<String> assessIds) {
        boolean result = false;
        if (CollUtil.isNotEmpty(assessIds)) {
            transformerParamService.remove(new LambdaQueryWrapper<AssessTransformerParam>().in(AssessTransformerParam::getAssessId, assessIds));
            capacitorParamService.remove(new LambdaQueryWrapper<AssessCapacitorParam>().in(AssessCapacitorParam::getAssessId, assessIds));
            windLoadParamService.remove(new LambdaQueryWrapper<AssessWindLoadParam>().in(AssessWindLoadParam::getAssessId, assessIds));
            loadParamService.remove(new LambdaQueryWrapper<AssessLoadParam>().in(AssessLoadParam::getAssessId, assessIds));
            shockLoadParamService.remove(new LambdaQueryWrapper<AssessShockLoadParam>().in(AssessShockLoadParam::getAssessId, assessIds));
            result = assessUserService.removeByIds(assessIds);
        }
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), result, CommonResponseEnum.SUCCESS.getMessage());
    }


}

