package com.yeejoin.amos.boot.module.jxiop.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yeejoin.amos.boot.biz.common.utils.DateUtils;
import com.yeejoin.amos.boot.module.common.api.enums.ExcelSelectData;
import com.yeejoin.amos.boot.module.jxiop.api.dto.HouseholdPvImport;
import com.yeejoin.amos.boot.module.jxiop.api.entity.HouseholdPv;
import com.yeejoin.amos.boot.module.jxiop.api.mapper.HouseholdPvMapper;
import com.yeejoin.amos.boot.module.jxiop.api.service.IHouseholdPvService;
import com.yeejoin.amos.boot.module.jxiop.api.dto.HouseholdPvDto;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTimeUtils;
import org.joda.time.LocalDateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.typroject.tyboot.core.rdbms.service.BaseService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 户用光伏表服务实现类
 *
 * @author system_generator
 * @date 2023-03-17
 */
@Service
public class HouseholdPvServiceImpl extends BaseService<HouseholdPvDto,HouseholdPv,HouseholdPvMapper> implements IHouseholdPvService {

    @Autowired
    HouseholdPvMapper householdPvMapper;


    /**
     * 分页查询
     */
    public IPage<HouseholdPv> queryForHouseholdPvPage(Page<HouseholdPv> page, HouseholdPvDto dto)   {
        LambdaQueryWrapper<HouseholdPv> lambda = new QueryWrapper<HouseholdPv>().lambda();
        if (!ObjectUtils.isEmpty(dto.getName())){
            lambda.like(HouseholdPv::getName, dto.getName());
        }
        if (!ObjectUtils.isEmpty(dto.getPlatformCompanyId())){
            lambda.eq(HouseholdPv::getPlatformCompanyId, dto.getPlatformCompanyId());
        }
        if (!ObjectUtils.isEmpty(dto.getAddress())){
            lambda.like(HouseholdPv::getAddress, dto.getAddress());
        }
        if (!ObjectUtils.isEmpty(dto.getNumber())){
            lambda.like(HouseholdPv::getNumber, dto.getNumber());
        }
        return householdPvMapper.selectPage(page, lambda);
    }

    /**
     * 列表查询 示例
     */
    public List<HouseholdPvDto> queryForHouseholdPvList()   {
        return this.queryForList("" , false);
    }

    @Override
    public void importData(List<HouseholdPvImport> list) {
        ArrayList<HouseholdPv> householdPvs = new ArrayList<>();
        list.forEach(item ->{
            HouseholdPv householdPv = new HouseholdPv();
            householdPv.setCreateTime(LocalDateTime.now().toDate());
            if (!ObjectUtils.isEmpty(item.getExpiryTime())){
                try {
                    Date date = DateUtils.dateParse(item.getExpiryTime(), DateUtils.DATE_PATTERN);
                    householdPv.setExpiryTime(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            BeanUtils.copyProperties(item,householdPv);
            householdPvs.add(householdPv);
        });
        this.saveBatch(householdPvs);
    }

    @Override
    public List<HouseholdPvImport> selectExportList(HouseholdPvDto dto) {
        return householdPvMapper.selectExportList(dto);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        householdPvMapper.deleteBatchIds(ids);
    }
}