package com.ltgk.port.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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 com.ltgk.port.domain.dto.ExportWaterAreaInfoReq;
import com.ltgk.port.domain.dto.SaveWaterAreaInfoReq;
import com.ltgk.port.domain.dto.WaterAreaInfoListReq;
import com.ltgk.port.domain.entity.WaterAreaInfo;
import com.ltgk.port.domain.vo.WaterAreaInfoVo;
import com.ltgk.port.mapper.WaterAreaInfoMapper;
import com.ltgk.port.service.WaterAreaInfoService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

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

@Service
public class WaterAreaInfoServiceImpl extends ServiceImpl<WaterAreaInfoMapper, WaterAreaInfo> implements WaterAreaInfoService {

    @Override
    public AjaxResult save(SaveWaterAreaInfoReq saveWaterAreaInfoReq) {
        WaterAreaInfo waterAreaInfo = new WaterAreaInfo();
        BeanUtils.copyProperties(saveWaterAreaInfoReq, waterAreaInfo);

        // 更新的时候不更新 创建者和创建时间
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        Date nowDate = DateUtils.getNowDate();

        if (StringUtils.isBlank(saveWaterAreaInfoReq.getId())) {
            waterAreaInfo.setCreateBy(userName);
            waterAreaInfo.setCreateAt(nowDate);
        }
        waterAreaInfo.setUpdateBy(userName);
        waterAreaInfo.setUpdateAt(nowDate);

        if (this.saveOrUpdate(waterAreaInfo)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public List<WaterAreaInfo> list(WaterAreaInfoListReq waterAreaInfoListReq) {
        waterAreaInfoListReq.queryCheck();
        WaterAreaInfo waterAreaInfo = new WaterAreaInfo();
        BeanUtils.copyProperties(waterAreaInfoListReq, waterAreaInfo);
        startPage(waterAreaInfoListReq.getPageNum(), waterAreaInfoListReq.getPageSize());
        LambdaQueryWrapper<WaterAreaInfo> lqw = new LambdaQueryWrapper<>(waterAreaInfo)
                .eq(WaterAreaInfo::getDelFlag, 0);
        return this.list(lqw);
    }

    @Override
    public void export(HttpServletResponse response, ExportWaterAreaInfoReq exportWaterAreaInfoReq) {
        exportWaterAreaInfoReq.queryCheck();
        WaterAreaInfo waterAreaInfo = new WaterAreaInfo();
        BeanUtils.copyProperties(exportWaterAreaInfoReq, waterAreaInfo);

        LambdaQueryWrapper<WaterAreaInfo> lqw = new LambdaQueryWrapper<>(waterAreaInfo)
                .eq(WaterAreaInfo::getDelFlag, 0);
        List<WaterAreaInfo> list = this.list(lqw);
        List<WaterAreaInfoVo> voList = list.stream().map(item -> {
            WaterAreaInfoVo waterAreaInfoVo = new WaterAreaInfoVo();
            BeanUtils.copyProperties(item, waterAreaInfoVo);
            return waterAreaInfoVo;
        }).collect(Collectors.toList());
        ExcelUtil<WaterAreaInfoVo> util = new ExcelUtil<>(WaterAreaInfoVo.class);
        util.exportExcel(response, voList, "水野保护区数据导出");
    }

    @Override
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WaterAreaInfoVo> util = new ExcelUtil<>(WaterAreaInfoVo.class);
        util.importTemplateExcel(response, "水野保护区数据导入模板");
    }

    @Override
    public AjaxResult importData(MultipartFile file) throws IOException {
        // 校验是否为 Excel 格式
        ExcelUtil.checkExcelFile(file);
        ExcelUtil<WaterAreaInfoVo> util = new ExcelUtil<>(WaterAreaInfoVo.class);
        List<WaterAreaInfoVo> list = util.importExcel(file.getInputStream());
        if (CollectionUtils.isEmpty(list) || list.stream().allMatch(Objects::isNull)) {
            return AjaxResult.error("导入数据为空或模板格式有误！");
        }

        List<WaterAreaInfo> infoList = list.stream().map(m -> {
            WaterAreaInfo waterAreaInfo = new WaterAreaInfo();
            BeanUtils.copyProperties(m, waterAreaInfo);
            if (ObjectUtils.isNotEmpty(waterAreaInfo.getId())) {
                waterAreaInfo.setUpdateBy(SecurityUtils.getUserId().toString());
                waterAreaInfo.setUpdateAt(DateUtils.getNowDate());
            } else {
                waterAreaInfo.setCreateBy(SecurityUtils.getUserId().toString());
                waterAreaInfo.setCreateAt(DateUtils.getNowDate());
            }
            return waterAreaInfo;
        }).collect(Collectors.toList());
        return this.saveOrUpdateBatch(infoList) ? AjaxResult.success() : AjaxResult.error();
    }
}
