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.GetBoatFishingListReq;
import com.ltgk.boat.domain.dto.SaveBoatFishingReq;
import com.ltgk.boat.domain.entity.DsBoatData;
import com.ltgk.boat.domain.entity.DsBoatFishing;
import com.ltgk.boat.mapper.DsBoatFishingMapper;
import com.ltgk.boat.service.DsBoatDataService;
import com.ltgk.boat.service.DsBoatFishingService;
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.StringUtils;
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:10
 * @Version: 1.0
 */
@Service
public class DsBoatFishingServiceImpl extends ServiceImpl<DsBoatFishingMapper, DsBoatFishing> implements DsBoatFishingService {
    @Autowired
    private DsBoatDataService dsBoatDataService;

    @DataAreaCodeSource()
    @Override
    public List<DsBoatFishing> getBoatFishingList(GetBoatFishingListReq getBoatFishingListReq) {
        startPage(getBoatFishingListReq.getPageNum(), getBoatFishingListReq.getPageSize());
        LambdaQueryWrapper<DsBoatFishing> lqw = new LambdaQueryWrapper<>();
        lqw.like(ObjectUtils.isNotEmpty(getBoatFishingListReq.getCode()), DsBoatFishing::getFishingCode, getBoatFishingListReq.getCode());
        lqw.like(ObjectUtils.isNotEmpty(getBoatFishingListReq.getBoatName()), DsBoatFishing::getBoatName, getBoatFishingListReq.getBoatName());
        lqw.between(ObjectUtils.isNotEmpty(getBoatFishingListReq.getStartTime()) && ObjectUtils.isNotEmpty(getBoatFishingListReq.getEndTime()), DsBoatFishing::getSignDate, getBoatFishingListReq.getStartTime(), getBoatFishingListReq.getEndTime());
        lqw.apply(ObjectUtils.isNotEmpty(getBoatFishingListReq.getAreaCodeCondition()), getBoatFishingListReq.getAreaCodeCondition());
        lqw.orderByDesc(DsBoatFishing::getCreateAt);
        return this.list(lqw);
    }

    @Override
    public AjaxResult saveBoatFishing(SaveBoatFishingReq saveBoatFishingReq) throws IllegalAccessException {
        saveBoatFishingReq.checkoutData();
        DsBoatFishing dsBoatFishing = new DsBoatFishing();
        BeanUtils.copyBeanProp(dsBoatFishing, saveBoatFishingReq);
        DsBoatData boat = dsBoatDataService.getBoatById(saveBoatFishingReq.getBoatId(), saveBoatFishingReq.getBoatName());
        BeanUtils.copyBeanProp(dsBoatFishing, boat);

        dsBoatFishing.setFishingCode(saveBoatFishingReq.getFishingCode())
                .setMainWorkType(saveBoatFishingReq.getMainWorkType())
                .setDoubleWorkType(saveBoatFishingReq.getDoubleWorkType())
                .setCertificateValidity(DateUtils.parseDate(saveBoatFishingReq.getCertificateValidity()));

        if (StringUtils.isBlank(dsBoatFishing.getId())) {
            dsBoatFishing.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
            dsBoatFishing.setCreateAt(DateUtils.getNowDate());
        }
        dsBoatFishing.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        dsBoatFishing.setUpdateAt(DateUtils.getNowDate());
        if (this.saveOrUpdate(dsBoatFishing)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public AjaxResult deleteBoatFishing(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));
    }
}
