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.AddDsCrewEnterCheckReq;
import com.ltgk.crew.domain.dto.DsCrewEnterCheckDTO;
import com.ltgk.crew.domain.dto.FindCrewCheckByPortIdReq;
import com.ltgk.crew.domain.entity.DsCrewEnterCheck;
import com.ltgk.crew.service.IDsCrewEnterCheckService;
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/dsCrewEnterCheck")
public class DsCrewEnterCheckController extends BaseController {

    @Resource
    private IDsCrewEnterCheckService dsCrewEnterCheckService;


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

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

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

    /**
     * 新增进港船员报备信息
     * -- 1. 不可重复提交
     * -- 2. 新增的船员不是系统中的船员时，先添加系统中船员的数据
     * -- 3. 校验此船员是否绑定其他渔船，如果绑定其他渔船，则不可进行提交
     * -- 4. 新增渔船与船员绑定信息
     * -- 5. 新增船员审核信息
     *
     * @param addDsCrewEnterCheckReq: 新增进港船员报备信息请求参数数据传输对象
     * @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(@Validated @RequestBody AddDsCrewEnterCheckReq addDsCrewEnterCheckReq) {
        return toAjax(dsCrewEnterCheckService.addDsCrewEnterCheck(addDsCrewEnterCheckReq));
    }

    /**
     * 修改进港船员报备信息
     *
     * @param dsCrewEnterCheck:
     * @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 DsCrewEnterCheck dsCrewEnterCheck) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        dsCrewEnterCheck.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(dsCrewEnterCheckService.updateById(dsCrewEnterCheck));
    }

    /**
     * 删除进港船员报备信息
     *
     * @param dsCrewEnterCheckDTO:
     * @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 DsCrewEnterCheckDTO dsCrewEnterCheckDTO) {
        return toAjax(dsCrewEnterCheckService.removeById(dsCrewEnterCheckDTO.getId()));
    }

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

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

    /**
     * 组装进港船员报备信息 wrapper
     *
     * @param dsCrewEnterCheckDTO:
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.ltgk.crew.domain.entity.DsCrewEnterCheck>
     * @author Qi ChengBin
     * @date 2024/12/17 15:36
     */
    private static LambdaQueryWrapper<DsCrewEnterCheck> getDsCrewEnterCheckLambdaQueryWrapper(DsCrewEnterCheckDTO dsCrewEnterCheckDTO) {
        dsCrewEnterCheckDTO.queryCheck();
        DsCrewEnterCheck dsCrewEnterCheck = new DsCrewEnterCheck();
        BeanUtils.copyProperties(dsCrewEnterCheckDTO, dsCrewEnterCheck);
        LambdaQueryWrapper<DsCrewEnterCheck> wrapper = new LambdaQueryWrapper<>(dsCrewEnterCheck);
        wrapper.orderByDesc(DsCrewEnterCheck::getUpdateAt);
        return wrapper;
    }
}
