package com.ltgk.port.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltgk.common.annotation.Log;
import com.ltgk.common.annotation.RepeatSubmit;
import com.ltgk.common.core.controller.BaseController;
import com.ltgk.common.core.domain.AjaxResult;
import com.ltgk.common.core.domain.entity.SysUser;
import com.ltgk.common.core.page.TableDataInfo;
import com.ltgk.common.enums.BusinessType;
import com.ltgk.common.utils.SecurityUtils;
import com.ltgk.common.utils.StringUtils;
import com.ltgk.common.utils.bean.BeanUtils;
import com.ltgk.common.utils.enums.EnumUtil;
import com.ltgk.common.utils.poi.ExcelUtil;
import com.ltgk.common.validation.groups.Add;
import com.ltgk.common.validation.groups.Detail;
import com.ltgk.common.validation.groups.Update;
import com.ltgk.port.domain.dto.PortEquRepairReq;
import com.ltgk.port.domain.entity.PortEquRepair;
import com.ltgk.port.enums.PortEquRepairStateEnums;
import com.ltgk.port.service.IPortEquRepairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 渔港设施设备报修管理 前端控制器
 *
 * @author Qi ChengBin
 * @date 2025-01-15
 */
@Api(tags = "渔港设施设备报修管理")
@RestController
@RequestMapping("/fishingPort/portEquRepair")
public class PortEquRepairController extends BaseController {

    @Resource
    private IPortEquRepairService portEquRepairService;

    /**
     * 查询所有鱼港设施设备报修管理状态枚举值
     *
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/2/17
     */
    @PostMapping("/findPortEquRepairStateEnumList")
    @ApiOperation(value = "获取鱼港设施设备报修管理状态枚举列表")
    public AjaxResult findPortEquRepairStateEnumList() {
        return success(EnumUtil.getEnumsVOList(PortEquRepairStateEnums.class));
    }

    /**
     * 分页查询渔港设施设备报修管理列表
     *
     * @param req: 渔港设施设备报修管理查询参数对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-01-15
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询渔港设施设备报修管理列表")
    public TableDataInfo page(@RequestBody PortEquRepairReq req) {
        LambdaQueryWrapper<PortEquRepair> wrapper = getPortEquRepairLambdaQueryWrapper(req);
        startPage(req.getPageNum(), req.getPageSize());
        List<PortEquRepair> portEquRepairList = portEquRepairService.list(wrapper);
        return getDataTable(portEquRepairList);
    }

    /**
     * 导出渔港设施设备报修管理列表
     *
     * @param req:渔港设施设备报修管理查询参数对象
     * @author Qi ChengBin
     * @date 2025-01-15
     */
    @Log(title = "渔港设施设备报修管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出渔港设施设备报修管理数据")
    public void export(HttpServletResponse response, PortEquRepairReq req) {
        LambdaQueryWrapper<PortEquRepair> wrapper = getPortEquRepairLambdaQueryWrapper(req);
        List<PortEquRepair> portEquRepairList = portEquRepairService.list(wrapper);
        ExcelUtil<PortEquRepair> util = new ExcelUtil<PortEquRepair>(PortEquRepair.class);
        util.exportExcel(response, portEquRepairList, "渔港设施设备报修管理数据");
    }

    /**
     * 获取渔港设施设备报修管理详细信息
     *
     * @param req:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-01-15
     */
    @PostMapping(value = "/detail")
    @ApiOperation(value = "获取渔港设施设备报修管理详细数据")
    public AjaxResult detail(@Validated(Detail.class) @RequestBody PortEquRepairReq req) {
        return success(portEquRepairService.getById(req.getId()));
    }

    /**
     * 新增渔港设施设备报修管理信息
     *
     * @param portEquRepair:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-01-15
     */
    @RepeatSubmit(message = "请勿重复提交")
    @Log(title = "新增渔港设施设备报修管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增渔港设施设备报修管理数据")
    public AjaxResult add(@Validated(Add.class) @RequestBody PortEquRepair portEquRepair) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        portEquRepair.setReportUserId(String.valueOf(user.getUserId()))
                .setReportUserName(user.getNickName())
                .setReportTime(new Date())
                .setCreateBy(user.getNickName())
                .setCreateAt(new Date())
                .setUpdateBy(user.getNickName())
                .setUpdateAt(new Date());
        return toAjax(portEquRepairService.save(portEquRepair));
    }

    /**
     * 修改渔港设施设备报修管理信息
     *
     * @param portEquRepair:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-01-15
     */
    @Log(title = "修改渔港设施设备报修管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改渔港设施设备报修管理数据")
    public AjaxResult edit(@Validated(Update.class) @RequestBody PortEquRepair portEquRepair) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        portEquRepair.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(portEquRepairService.updateById(portEquRepair));
    }

    /**
     * 维修人员维修渔港设施设备
     *
     * @param portEquRepair:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/1/15 20:16
     */
    @Log(title = "维修", businessType = BusinessType.UPDATE)
    @PostMapping("/repair")
    @ApiOperation(value = "维修人员维修")
    public AjaxResult repair(@RequestBody PortEquRepair portEquRepair) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        portEquRepair
                .setRepairState(PortEquRepairStateEnums.REPAIRED.getCode())
                .setRepairUserId(String.valueOf(user.getUserId()))
                .setRepairUserName(user.getNickName())
                .setRepairTime(new Date())
                .setUpdateBy(user.getNickName())
                .setUpdateAt(new Date());
        return toAjax(portEquRepairService.updateById(portEquRepair));
    }

    /**
     * 确认渔港设施设备报修管理
     *
     * @param portEquRepair:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/1/15 20:31
     */
    @Log(title = "确认渔港设施设备报修管理", businessType = BusinessType.UPDATE)
    @PostMapping("/confirmed")
    @ApiOperation(value = "确认渔港设施设备报修管理数据")
    public AjaxResult confirmed(@RequestBody PortEquRepair portEquRepair) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        portEquRepair.setConfirmUserId(String.valueOf(user.getUserId()))
                .setConfirmUserName(user.getNickName())
                .setConfirmTime(new Date())
                .setUpdateBy(user.getNickName())
                .setUpdateAt(new Date());
        return toAjax(portEquRepairService.updateById(portEquRepair));
    }

    /**
     * 删除渔港设施设备报修管理信息
     *
     * @param req:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-01-15
     */
    @Log(title = "删除渔港设施设备报修管理", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "批量删除渔港设施设备报修管理数据")
    public AjaxResult delete(@Validated(Detail.class) @RequestBody PortEquRepairReq req) {
        return toAjax(portEquRepairService.removeById(req.getId()));
    }

    /**
     * 根据渔港设施设备管理查询参数对象获取 wrapper
     *
     * @param req: 渔港设施设备报修管理查询参数对象
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.ltgk.port.domain.entity.PortEquRepair>
     * @author Qi ChengBin
     * @date 2025/1/15 19:47
     */
    private static LambdaQueryWrapper<PortEquRepair> getPortEquRepairLambdaQueryWrapper(PortEquRepairReq req) {
        req.queryCheck();
        PortEquRepair portEquRepair = new PortEquRepair();
        BeanUtils.copyProperties(req, portEquRepair);

        String startTime = req.getStartTime();
        String endTime = req.getEndTime();

        return new LambdaQueryWrapper<>(portEquRepair)
                .between(StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime), PortEquRepair::getReportTime, startTime, endTime)
                .orderByAsc(PortEquRepair::getDamageTime);
    }
}
