package com.ltgk.boat.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltgk.boat.domain.dto.DeleteByIdsReq;
import com.ltgk.boat.domain.dto.GetBoatNameListReq;
import com.ltgk.boat.domain.dto.SaveBoatNameReq;
import com.ltgk.boat.domain.entity.DsBoatData;
import com.ltgk.boat.domain.entity.DsBoatName;
import com.ltgk.boat.mapper.DsBoatNameMapper;
import com.ltgk.boat.service.DsBoatDataService;
import com.ltgk.boat.service.DsBoatNameService;
import com.ltgk.common.annotation.DataAreaCodeSource;
import com.ltgk.common.constant.ReturnMsgConstants;
import com.ltgk.common.core.domain.AjaxResult;
import com.ltgk.common.utils.DateUtils;
import com.ltgk.common.utils.SecurityUtils;
import com.ltgk.common.utils.bean.BeanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
 * @Author: wan kun
 * @Description: 船名登记信息表
 * @Date: 2024/12/24 17:36
 * @Version: 1.0
 */
@Service
public class DsBoatNameServiceImpl extends ServiceImpl<DsBoatNameMapper, DsBoatName> implements DsBoatNameService {
    @Autowired
    private DsBoatDataService dsBoatDataService;

    @DataAreaCodeSource()
    @Override
    public List<DsBoatName> getBoatNameList(GetBoatNameListReq getBoatNameListReq) {
        startPage(getBoatNameListReq.getPageNum(), getBoatNameListReq.getPageSize());
        LambdaQueryWrapper<DsBoatName> lqw = new LambdaQueryWrapper<>();
        lqw.like(ObjectUtils.isNotEmpty(getBoatNameListReq.getCode()), DsBoatName::getBoatNameApprovalCode, getBoatNameListReq.getCode());
        lqw.like(ObjectUtils.isNotEmpty(getBoatNameListReq.getBoatName()), DsBoatName::getBoatName, getBoatNameListReq.getBoatName());
        lqw.between(ObjectUtils.isNotEmpty(getBoatNameListReq.getStartTime()) && ObjectUtils.isNotEmpty(getBoatNameListReq.getEndTime()), DsBoatName::getApplyDate, getBoatNameListReq.getStartTime(), getBoatNameListReq.getEndTime());
        lqw.apply(ObjectUtils.isNotEmpty(getBoatNameListReq.getAreaCodeCondition()), getBoatNameListReq.getAreaCodeCondition());
        lqw.orderByDesc(DsBoatName::getCreateAt);
        return this.list(lqw);
    }

    @Override
    public AjaxResult saveBoatName(SaveBoatNameReq saveBoatNameReq) throws IllegalAccessException {
        saveBoatNameReq.checkoutData();
        DsBoatName dsBoatName = new DsBoatName();
        BeanUtils.copyBeanProp(dsBoatName, saveBoatNameReq);
        DsBoatData boat = dsBoatDataService.getBoatById(saveBoatNameReq.getBoatId(), saveBoatNameReq.getBoatName());
        BeanUtils.copyBeanProp(dsBoatName, boat);
        dsBoatName.setCertificateValidity(DateUtils.parseDate(saveBoatNameReq.getCertificateValidity()));
        if (ObjectUtils.isNotEmpty(dsBoatName.getId())) {
            dsBoatName.setUpdateBy(SecurityUtils.getUserId().toString());
            dsBoatName.setUpdateAt(DateUtils.getNowDate());
        } else {
            dsBoatName.setCreateBy(SecurityUtils.getUserId().toString());
            dsBoatName.setCreateAt(DateUtils.getNowDate());
        }
        if (this.saveOrUpdate(dsBoatName)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public AjaxResult deleteBoatName(DeleteByIdsReq deleteByIdsReq) {
        List<String> idList = Arrays.asList(deleteByIdsReq.getIds().split(","));
        if (CollUtil.isEmpty(idList)) {
            return AjaxResult.error(ReturnMsgConstants.DELETE_ID_NOT_EMPTY);
        }
        return AjaxResult.success(this.removeByIds(idList));
    }
}
