package com.ltgk.boat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltgk.boat.domain.dto.BoatDisciplineExcelReq;
import com.ltgk.boat.domain.dto.GetBoatDisciplineListReq;
import com.ltgk.boat.domain.dto.SaveBoatDisciplineReq;
import com.ltgk.boat.domain.entity.DsBoatDiscipline;
import com.ltgk.boat.domain.vo.BoatDisciplineVo;
import com.ltgk.boat.mapper.DsBoatDisciplineMapper;
import com.ltgk.boat.service.DsBoatDataService;
import com.ltgk.boat.service.DsBoatDisciplineService;
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 com.ltgk.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

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

@Service
public class DsBoatDisciplineServiceImpl extends ServiceImpl<DsBoatDisciplineMapper, DsBoatDiscipline> implements DsBoatDisciplineService {
    @Autowired
    private DsBoatDataService dsBoatDataService;

    @Override
    public AjaxResult save(SaveBoatDisciplineReq saveBoatDisciplineReq) {
        DsBoatDiscipline dsBoatDiscipline = new DsBoatDiscipline();
        BeanUtils.copyProperties(saveBoatDisciplineReq, dsBoatDiscipline);
        dsBoatDiscipline.setIdentificationDate(DateUtils.parseDate(saveBoatDisciplineReq.getIdentificationDate()))
                .setEndDate(DateUtils.parseDate(saveBoatDisciplineReq.getEndDate()));

        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        if (StringUtils.isBlank(saveBoatDisciplineReq.getId())) {
            dsBoatDiscipline.setCreateBy(userName);
            dsBoatDiscipline.setCreateAt(DateUtils.getNowDate());
        }
        dsBoatDiscipline.setUpdateBy(userName);
        dsBoatDiscipline.setUpdateAt(DateUtils.getNowDate());
        return AjaxResult.success(this.saveOrUpdate(dsBoatDiscipline));
    }

    @Override
    public List<DsBoatDiscipline> list(GetBoatDisciplineListReq getBoatDisciplineListReq) {
        startPage(getBoatDisciplineListReq.getPageNum(), getBoatDisciplineListReq.getPageSize());
        LambdaQueryWrapper<DsBoatDiscipline> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(getBoatDisciplineListReq.getBoatName()), DsBoatDiscipline::getBoatName, getBoatDisciplineListReq.getBoatName());
        lqw.like(ObjectUtils.isNotEmpty(getBoatDisciplineListReq.getBoatOwner()), DsBoatDiscipline::getBoatOwner, getBoatDisciplineListReq.getBoatOwner());
        lqw.eq(ObjectUtils.isNotEmpty(getBoatDisciplineListReq.getDisciplinaryGrade()), DsBoatDiscipline::getDisciplinaryGrade, getBoatDisciplineListReq.getDisciplinaryGrade());
        lqw.gt(ObjectUtils.isNotEmpty(getBoatDisciplineListReq.getStartTime()), DsBoatDiscipline::getIdentificationDate, getBoatDisciplineListReq.getStartTime());
        lqw.lt(ObjectUtils.isNotEmpty(getBoatDisciplineListReq.getEndTime()), DsBoatDiscipline::getIdentificationDate, getBoatDisciplineListReq.getEndTime());
        lqw.orderByDesc(DsBoatDiscipline::getCreateAt);
        return this.list(lqw);
    }

    @Override
    public void exportExcel(HttpServletResponse response, BoatDisciplineExcelReq boatDisciplineExcelReq) {
        LambdaQueryWrapper<DsBoatDiscipline> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(boatDisciplineExcelReq.getBoatName()), DsBoatDiscipline::getBoatName, boatDisciplineExcelReq.getBoatName());
        lqw.like(ObjectUtils.isNotEmpty(boatDisciplineExcelReq.getBoatOwner()), DsBoatDiscipline::getBoatOwner, boatDisciplineExcelReq.getBoatOwner());
        lqw.eq(ObjectUtils.isNotEmpty(boatDisciplineExcelReq.getDisciplinaryGrade()), DsBoatDiscipline::getDisciplinaryGrade, boatDisciplineExcelReq.getDisciplinaryGrade());
        lqw.gt(ObjectUtils.isNotEmpty(boatDisciplineExcelReq.getStartTime()), DsBoatDiscipline::getIdentificationDate, boatDisciplineExcelReq.getStartTime());
        lqw.lt(ObjectUtils.isNotEmpty(boatDisciplineExcelReq.getEndTime()), DsBoatDiscipline::getIdentificationDate, boatDisciplineExcelReq.getEndTime());
        lqw.orderByDesc(DsBoatDiscipline::getCreateAt);
        List<DsBoatDiscipline> list = this.list(lqw);
        ArrayList<BoatDisciplineVo> exportList = new ArrayList<>();
        list.forEach(item -> {
            BoatDisciplineVo vo = new BoatDisciplineVo();
            BeanUtils.copyBeanProp(vo, item);
            if (ObjectUtils.isNotEmpty(item.getIdentificationDate()))
                vo.setIdentificationDate(DateUtils.dateTimeDDHHMMSS(item.getIdentificationDate()));
            if (ObjectUtils.isNotEmpty(item.getEndDate()))
                vo.setEndDate(DateUtils.dateTimeDDHHMMSS(item.getEndDate()));
            exportList.add(vo);
        });
        ExcelUtil<BoatDisciplineVo> util = new ExcelUtil<>(BoatDisciplineVo.class);
        util.exportExcel(response, exportList, "联合惩戒");
    }
}
