package com.ltgk.crew.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.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.poi.ExcelUtil;
import com.ltgk.crew.domain.dto.AddDsCrewOutCheckReq;
import com.ltgk.crew.domain.dto.DsCrewOutCheckDTO;
import com.ltgk.crew.domain.dto.FindCrewCheckByPortIdReq;
import com.ltgk.crew.domain.entity.DsCrewOutCheck;
import com.ltgk.crew.service.IDsCrewOutCheckService;
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;

/**
 * 出港船员报备信息Controller
 *
 * @author Qi ChengBin
 * @date 2024-12-07
 */
@Api(tags = "出港船员报备信息")
@RestController
@RequestMapping("/fishingPort/dsCrewOutCheck")
@Validated
public class DsCrewOutCheckController extends BaseController {

    @Resource
    private IDsCrewOutCheckService dsCrewOutCheckService;

    /**
     * 分页查询出港船员报备信息列表
     *
     * @param dsCrewOutCheckDTO: 出港船员报备信息传输对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2024-12-07
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询出港船员报备信息列表")
    public TableDataInfo page(@RequestBody DsCrewOutCheckDTO dsCrewOutCheckDTO) {
        LambdaQueryWrapper<DsCrewOutCheck> wrapper = getDsCrewOutCheckLambdaQueryWrapper(dsCrewOutCheckDTO);
        // 查询24h出港船员列表  -- 只传 预计出港时间【24h前的时间】  年-月-日 时:分:秒的形式
        wrapper.ge(StringUtils.isNotBlank(dsCrewOutCheckDTO.getOutTime()), DsCrewOutCheck::getOutTime, dsCrewOutCheckDTO.getOutTime());
        startPage(dsCrewOutCheckDTO.getPageNum(), dsCrewOutCheckDTO.getPageSize());
        List<DsCrewOutCheck> dsCrewOutCheckList = dsCrewOutCheckService.list(wrapper);
        return getDataTable(dsCrewOutCheckList);
    }

    /**
     * 导出出港船员报备信息列表
     *
     * @param dsCrewOutCheckDTO:
     * @author Qi ChengBin
     * @date 2024-12-07
     */
    @Log(title = "出港船员报备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出出港船员报备信息数据")
    public void export(HttpServletResponse response, @RequestBody DsCrewOutCheckDTO dsCrewOutCheckDTO) {
        LambdaQueryWrapper<DsCrewOutCheck> wrapper = getDsCrewOutCheckLambdaQueryWrapper(dsCrewOutCheckDTO);
        List<DsCrewOutCheck> dsCrewOutCheckList = dsCrewOutCheckService.list(wrapper);
        ExcelUtil<DsCrewOutCheck> util = new ExcelUtil<DsCrewOutCheck>(DsCrewOutCheck.class);
        util.exportExcel(response, dsCrewOutCheckList, "出港船员报备信息数据");
    }

    /**
     * 获取出港船员报备信息详细信息
     *
     * @param dsCrewOutCheckDTO:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024-12-07
     */
    @PostMapping(value = "/detail")
    @ApiOperation(value = "获取出港船员报备信息详细数据")
    public AjaxResult detail(@RequestBody DsCrewOutCheckDTO dsCrewOutCheckDTO) {
        return success(dsCrewOutCheckService.getById(dsCrewOutCheckDTO.getId()));
    }

    /**
     * 新增出港船员报备信息
     * -- 1. 不可重复提交
     * -- 2. 新增的船员不是系统中的船员时，先添加系统中船员的数据
     * -- 3. 校验此船员是否绑定其他渔船，如果绑定其他渔船，则不可进行提交
     * -- 4. 新增渔船与船员绑定信息
     * -- 5. 新增船员审核信息
     *
     * @param addDsCrewOutCheckReq: 船员出港审核数据传输参数
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024-12-07
     */
    @RepeatSubmit(message = "请勿重复提交")
    @Log(title = "新增出港船员报备信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增出港船员报备信息数据")
    public AjaxResult add(@RequestBody AddDsCrewOutCheckReq addDsCrewOutCheckReq) {
        return toAjax(dsCrewOutCheckService.addDsCrewOutCheck(addDsCrewOutCheckReq));
    }

    /**
     * 修改出港船员报备信息
     *
     * @param dsCrewOutCheck:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024-12-07
     */
    @Log(title = "修改出港船员报备信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改出港船员报备信息数据")
    public AjaxResult edit(@RequestBody DsCrewOutCheck dsCrewOutCheck) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        dsCrewOutCheck.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(dsCrewOutCheckService.updateById(dsCrewOutCheck));
    }

    /**
     * 删除出港船员报备信息
     *
     * @param dsCrewOutCheckDTO:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024-12-07
     */
    @Log(title = "删除出港船员报备信息", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除出港船员报备信息数据")
    public AjaxResult delete(@RequestBody DsCrewOutCheckDTO dsCrewOutCheckDTO) {
        return toAjax(dsCrewOutCheckService.removeById(dsCrewOutCheckDTO.getId()));
    }

    /**
     * 根据出港信息ID 查询 出港 船员信息列表
     *
     * @param findCrewCheckByPortIdReq:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024/12/7 10:40
     */
    @PostMapping("/findCrewOutCheckByOutPortId")
    @ApiOperation("根据出港信息ID查询出港船员信息")
    public AjaxResult findCrewOutCheckByOutPortId(@Validated @RequestBody FindCrewCheckByPortIdReq findCrewCheckByPortIdReq) {
        LambdaQueryWrapper<DsCrewOutCheck> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DsCrewOutCheck::getOutPortId, findCrewCheckByPortIdReq.getId())
                .orderByDesc(DsCrewOutCheck::getCreateAt);
        return AjaxResult.success(dsCrewOutCheckService.list(wrapper));
    }

    /**
     * 出港报信信息审核
     *
     * @param dsCrewOutCheckDTO: 出港报备信息
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024/12/7 10:46
     */
    @PostMapping("/check")
    @ApiOperation("出港船员报备信息审核")
    public AjaxResult check(@RequestBody DsCrewOutCheckDTO dsCrewOutCheckDTO) {
        String reason = dsCrewOutCheckDTO.getReason();
        String checkUrl = dsCrewOutCheckDTO.getCheckUrl();
        UpdateWrapper<DsCrewOutCheck> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", dsCrewOutCheckDTO.getStatus())
                .set(StringUtils.isNotBlank(reason), "reason", reason)
                .set(StringUtils.isNotBlank(checkUrl), "check_url", checkUrl)
                .eq("id", dsCrewOutCheckDTO.getId());
        return toAjax(dsCrewOutCheckService.update(updateWrapper));
    }

    /**
     * 组装出港查询 wrapper
     *
     * @param dsCrewOutCheckDTO:
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.ltgk.crew.domain.entity.DsCrewOutCheck>
     * @author Qi ChengBin
     * @date 2024/12/17 15:16
     */
    private static LambdaQueryWrapper<DsCrewOutCheck> getDsCrewOutCheckLambdaQueryWrapper(DsCrewOutCheckDTO dsCrewOutCheckDTO) {
        dsCrewOutCheckDTO.queryCheck();
        DsCrewOutCheck dsCrewOutCheck = new DsCrewOutCheck();
        BeanUtils.copyProperties(dsCrewOutCheckDTO, dsCrewOutCheck);

        LambdaQueryWrapper<DsCrewOutCheck> wrapper = new LambdaQueryWrapper<>(dsCrewOutCheck);
        wrapper.orderByDesc(DsCrewOutCheck::getUpdateAt);
        return wrapper;
    }
}
