package com.ltgk.crew.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.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.validation.groups.Add;
import com.ltgk.common.validation.groups.Detail;
import com.ltgk.common.validation.groups.Update;
import com.ltgk.crew.domain.dto.DsCrewCredentialDTO;
import com.ltgk.crew.domain.entity.DsCrewCredential;
import com.ltgk.crew.service.IDsCrewCredentialService;
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 java.util.Date;
import java.util.Objects;

/**
 * 船员证件管理 前端控制器
 *
 * @author Qi ChengBin
 * @date 2024/12/04 18:01:33
 */
@RestController
@RequestMapping("/fishingPort/dsCrewCredential")
@Api(tags = "船员证件管理")
public class DsCrewCredentialController extends BaseController {

    @Resource
    private IDsCrewCredentialService dsCrewCredentialService;

    /**
     * 分页查询船员证书信息
     *
     * @param dsCrewCredentialDTO: 船员证书查询对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2024/12/5 09:00
     */
    @ApiOperation(value = "分页查询船员证书信息")
    @PostMapping("/page")
    @Log(title = "分页查询船员证书信息", businessType = BusinessType.PAGE_QUERY)
    public TableDataInfo page(@RequestBody DsCrewCredentialDTO dsCrewCredentialDTO) {
        DsCrewCredential dsCrewCredential = getDsCrewCredential(dsCrewCredentialDTO);
        // 开始时间和结束时间分别对应 证书开始时间和证书结束时间
        String startTime = dsCrewCredentialDTO.getStartTime();
        String endTime = dsCrewCredentialDTO.getEndTime();

        // 查询船员证书过期数据使用
        dsCrewCredential.setCertificateExpiredDate(null);

        LambdaQueryWrapper<DsCrewCredential> wrapper = new LambdaQueryWrapper<>(dsCrewCredential);
        wrapper.ge(StringUtils.isNotBlank(startTime), DsCrewCredential::getCertificateIssuedDate, startTime)
                .le(StringUtils.isNotBlank(endTime), DsCrewCredential::getCertificateExpiredDate, endTime)
                // 查询船员证书过期的数据，只传证书截至时间【年月日的形式，前端传当前日期，别的都不传】
                .lt(Objects.nonNull(dsCrewCredentialDTO.getCertificateExpiredDate()), DsCrewCredential::getCertificateExpiredDate, dsCrewCredentialDTO.getCertificateExpiredDate())
                .orderByDesc(DsCrewCredential::getUpdateAt);
        startPage(dsCrewCredentialDTO.getPageNum(), dsCrewCredentialDTO.getPageSize());
        return getDataTable(dsCrewCredentialService.list(wrapper));
    }

    /**
     * 新增船员证书信息
     *
     * @param dsCrewCredential: 船员证书实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024/12/24 10:00
     */
    @RepeatSubmit(message = "请勿重复提交")
    @ApiOperation(value = "添加船员证书信息")
    @PostMapping("/add")
    @Log(title = "添加船员证书信息", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody @Validated(Add.class) DsCrewCredential dsCrewCredential) {
        return toAjax(dsCrewCredentialService.saveDsCrewCredential(dsCrewCredential));
    }

    /**
     * 编辑船员证书信息
     *
     * @param dsCrewCredential: 船员证书数据实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024/12/5 09:26
     */
    @PostMapping("/update")
    @ApiOperation("编辑船员证书信息")
    @Log(title = "编辑船员证书信息", businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody @Validated(Update.class) DsCrewCredential dsCrewCredential) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        dsCrewCredential.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(dsCrewCredentialService.updateById(dsCrewCredential));
    }

    /**
     * 删除船员证书信息
     *
     * @param dsCrewCredentialDTO: 船员证书数据传输对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024/12/5 09:29
     */
    @ApiOperation(value = "删除船员证书信息")
    @PostMapping("/delete")
    @Log(title = "删除船员证书信息", businessType = BusinessType.DELETE)
    public AjaxResult delete(@RequestBody @Validated(Detail.class) DsCrewCredentialDTO dsCrewCredentialDTO) {
        return AjaxResult.success(dsCrewCredentialService.removeById(dsCrewCredentialDTO.getId()));
    }

    /**
     * 通过船员证书数据传输对象转换成船员证书信息对象
     *
     * @param dsCrewCredentialDTO:
     * @return com.ltgk.crew.domain.entity.DsCrewCredential
     * @author Qi ChengBin
     * @date 2024/12/24 09:56
     */
    private static DsCrewCredential getDsCrewCredential(DsCrewCredentialDTO dsCrewCredentialDTO) {
        dsCrewCredentialDTO.queryCheck();
        DsCrewCredential dsCrewCredential = new DsCrewCredential();
        BeanUtils.copyProperties(dsCrewCredentialDTO, dsCrewCredential);
        return dsCrewCredential;
    }
}
