package org.jeecg.modules.database.controller;

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-21
 * @Version: V1.0
 */
@Tag(name = "退火过程")
@RestController
@RequestMapping("/database/experimentAnnealProcess")
@Slf4j
public class ExperimentAnnealProcessController extends JeecgController<ExperimentAnnealProcess, IExperimentAnnealProcessService> {
    @Autowired
    private IExperimentAnnealProcessService experimentAnnealProcessService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IExperimentService    experimentService;
    @Autowired
    private IExperimentLogService experimentLogService;

    @Autowired
    private IExperimentSampleInfoService         experimentSampleInfoService;
    @Autowired
    private IExperimentDeviationConditionService experimentDeviationConditionService;
    @Autowired
    private IExperimentDeviationEquipmentService experimentDeviationEquipmentService;

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

    /**
     * 添加
     *
     * @param experimentAnnealProcess
     * @return
     */
    @AutoLog(value = "退火过程-添加")
    @Operation(summary = "退火过程-添加")
    @RequiresPermissions("database:experiment_anneal_process:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ExperimentAnnealProcess experimentAnnealProcess, HttpServletRequest request) {
        String experimentId = experimentAnnealProcess.getExperimentId();
        experimentAnnealProcess.setId(null);
        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);

        experimentAnnealProcessService.saveWithDetails(experimentAnnealProcess,"add");

        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 experimentAnnealProcess
     * @return
     */
    @AutoLog(value = "退火过程-编辑")
    @Operation(summary = "退火过程-编辑")
    @RequiresPermissions("database:experiment_anneal_process:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ExperimentAnnealProcess experimentAnnealProcess, HttpServletRequest request) {
        ExperimentAnnealProcess byId = experimentAnnealProcessService.getById(experimentAnnealProcess.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);

        experimentAnnealProcessService.saveWithDetails(experimentAnnealProcess,"edit");

        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_anneal_process:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id, HttpServletRequest request) {
        ExperimentAnnealProcess experimentAnnealProcess = experimentAnnealProcessService.getById(id);
        String experimentId = experimentAnnealProcess.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);

        experimentAnnealProcessService.removeById(id);

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

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

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

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

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ExperimentAnnealProcess experimentAnnealProcess) {
        return super.exportXls(request, experimentAnnealProcess, ExperimentAnnealProcess.class, "退火过程");
    }

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

}
