package org.jeecg.modules.database.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.database.constant.ExperimentStatus;
import org.jeecg.modules.database.entity.*;
import org.jeecg.modules.database.service.*;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Objects;

/**
 * @Description: 辐照过程
 * @Author: jeecg-boot
 * @Date: 2024-10-15
 * @Version: V1.0
 */
@Tag(name = "辐照过程")
@RestController
@RequestMapping("/database/experimentRadiationProcess")
@Slf4j
public class ExperimentRadiationProcessController extends JeecgController<ExperimentRadiationProcess, IExperimentRadiationProcessService> {
    @Autowired
    private IExperimentRadiationProcessService experimentRadiationProcessService;

    @Autowired
    private IExperimentLogService experimentLogService;
    @Autowired
    private ISysUserService       userService;

    @Autowired
    private IExperimentService experimentService;

    @Autowired
    private IExperimentSampleInfoService experimentSampleInfoService;

    /**
     * 分页列表查询
     *
     * @param experimentRadiationProcess
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "辐照过程-分页列表查询")
    @Operation(summary = "辐照过程-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ExperimentRadiationProcess>> queryPageList(ExperimentRadiationProcess experimentRadiationProcess,
                                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                   HttpServletRequest req) {
        QueryWrapper<ExperimentRadiationProcess> queryWrapper = QueryGenerator.initQueryWrapper(experimentRadiationProcess,
                req.getParameterMap());
        Page<ExperimentRadiationProcess> page = new Page<ExperimentRadiationProcess>(pageNo, pageSize);
        IPage<ExperimentRadiationProcess> pageList = experimentRadiationProcessService.page(page, queryWrapper);
        pageList.getRecords().forEach(v -> experimentRadiationProcessService.assembleDetails(v));
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param experimentRadiationProcess
     * @return
     */
    @AutoLog(value = "辐照过程-添加")
    @Operation(summary = "辐照过程-添加")
    @RequiresPermissions("database:experiment_radiation_process:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ExperimentRadiationProcess experimentRadiationProcess, HttpServletRequest request) {
        String experimentId = experimentRadiationProcess.getExperimentId();
        if (StringUtils.isBlank(experimentId)) {
            return Result.error("试验数据不存在！");
        }
        Experiment dbExperiment = experimentService.getById(experimentId);
        if (Objects.isNull(dbExperiment)) {
            return Result.error("试验数据不存在！");
        }
        if (StringUtils.equals(dbExperiment.getStatus(), ExperimentStatus.COMPLETED) || StringUtils.equals(dbExperiment.getStatus(),
                ExperimentStatus.REVIEWED)) {
            return Result.error("试验状态:[" + dbExperiment.getStatus() + "]不能进行修改！");
        }
        experimentRadiationProcess.setId(null);

        experimentService.changeStatus(experimentId, ExperimentStatus.PROGRESSING);
        experimentRadiationProcessService.save(experimentRadiationProcess);
        experimentRadiationProcessService.saveWithDetails(experimentRadiationProcess);

        ExperimentLog experimentLog = new ExperimentLog();
        experimentLog.setExperimentId(experimentId);
        experimentLog.setLogContent("添加辐照过程");
        String username = JwtUtil.getUserNameByToken(request);
        SysUser userByName = userService.getUserByName(username);
        experimentLog.setCreateBy(userByName.getRealname());
        experimentLogService.save(experimentLog);

        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param experimentRadiationProcess
     * @return
     */
    @AutoLog(value = "辐照过程-编辑")
    @Operation(summary = "辐照过程-编辑")
    @RequiresPermissions("database:experiment_radiation_process:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ExperimentRadiationProcess experimentRadiationProcess, HttpServletRequest request) {
        ExperimentRadiationProcess byId = experimentRadiationProcessService.getById(experimentRadiationProcess.getId());
        String experimentId = byId.getExperimentId();
        if (StringUtils.isBlank(experimentId)) {
            return Result.error("试验数据不存在！");
        }
        Experiment dbExperiment = experimentService.getById(experimentId);
        if (Objects.isNull(dbExperiment)) {
            return Result.error("试验数据不存在！");
        }
        if (StringUtils.equals(dbExperiment.getStatus(), ExperimentStatus.COMPLETED) || StringUtils.equals(dbExperiment.getStatus(),
                ExperimentStatus.REVIEWED)) {
            return Result.error("试验状态:[" + dbExperiment.getStatus() + "]不能进行修改！");
        }
        experimentService.changeStatus(experimentId, ExperimentStatus.PROGRESSING);
        experimentRadiationProcessService.saveOrUpdate(experimentRadiationProcess);

        experimentRadiationProcessService.saveWithDetails(experimentRadiationProcess);

        ExperimentLog experimentLog = new ExperimentLog();
        experimentLog.setExperimentId(experimentId);
        experimentLog.setLogContent("编辑辐照过程");
        String username = JwtUtil.getUserNameByToken(request);
        SysUser userByName = userService.getUserByName(username);
        experimentLog.setCreateBy(userByName.getRealname());
        experimentLogService.save(experimentLog);

        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "辐照过程-通过id删除")
    @Operation(summary = "辐照过程-通过id删除")
    @RequiresPermissions("database:experiment_radiation_process:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id, HttpServletRequest request) {
        ExperimentRadiationProcess experimentRadiationProcess = experimentRadiationProcessService.getById(id);
        String experimentId = experimentRadiationProcess.getExperimentId();
        if (StringUtils.isBlank(experimentId)) {
            return Result.error("试验数据不存在！");
        }
        Experiment dbExperiment = experimentService.getById(experimentId);
        if (Objects.isNull(dbExperiment)) {
            return Result.error("试验数据不存在！");
        }
        if (StringUtils.equals(dbExperiment.getStatus(), ExperimentStatus.COMPLETED) || StringUtils.equals(dbExperiment.getStatus(),
                ExperimentStatus.REVIEWED)) {
            return Result.error("试验状态:[" + dbExperiment.getStatus() + "]不能进行修改！");
        }
        ExperimentLog experimentLog = new ExperimentLog();
        experimentLog.setExperimentId(experimentId);
        experimentLog.setLogContent("删除辐照过程");
        String username = JwtUtil.getUserNameByToken(request);
        SysUser userByName = userService.getUserByName(username);
        experimentLog.setCreateBy(userByName.getRealname());
        experimentLogService.save(experimentLog);
        experimentRadiationProcessService.removeById(id);

        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "辐照过程-批量删除")
    @Operation(summary = "辐照过程-批量删除")
    @RequiresPermissions("database:experiment_radiation_process:delete")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.experimentRadiationProcessService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "辐照过程-通过id查询")
    @Operation(summary = "辐照过程-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ExperimentRadiationProcess> queryById(@RequestParam(name = "id", required = true) String id) {
        ExperimentRadiationProcess experimentRadiationProcess = experimentRadiationProcessService.getById(id);
        if (experimentRadiationProcess == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(experimentRadiationProcess);
    }

    /**
     * 通过实验id查询
     *
     * @param experimentalId
     * @return
     */
    //@AutoLog(value = "试验评定-通过id查询")
    @Operation(summary = "辐照过程-通过实验ID查询")
    @GetMapping(value = "/queryByExperimentId")
    public Result<ExperimentRadiationProcess> queryByExperimentalId(
            @RequestParam(name = "experimentId", required = true) String experimentalId) {
        LambdaQueryWrapper<ExperimentRadiationProcess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExperimentRadiationProcess::getExperimentId, experimentalId);
        ExperimentRadiationProcess experimentReview = experimentRadiationProcessService.getOne(queryWrapper);
        if (experimentReview == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(experimentReview);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param experimentRadiationProcess
     */

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ExperimentRadiationProcess experimentRadiationProcess) {
        return super.exportXls(request, experimentRadiationProcess, ExperimentRadiationProcess.class, "辐照过程");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("database:experiment_radiation_process:add")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ExperimentRadiationProcess.class);
    }

}
