package com.usky.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.website.domain.SiteReview;
import com.usky.website.mapper.SiteReviewMapper;
import com.usky.website.service.SiteReviewService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.website.service.vo.SiteReviewExportVO;
import com.usky.website.service.vo.SiteReviewRequestVO;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 官网_客户信息反馈表 服务实现类
 * </p>
 *
 * @author zyj
 * @since 2024-01-12
 */
@Service
public class SiteReviewServiceImpl extends AbstractCrudService<SiteReviewMapper, SiteReview> implements SiteReviewService {

    @Override
    public void add(SiteReview siteReview){
        siteReview.setCreateTime(LocalDateTime.now());
        siteReview.setStatus(1);
        this.save(siteReview);
    }

    @Override
    public CommonPage<SiteReview> page(SiteReviewRequestVO requestVO){
        Integer current = requestVO.getCurrent();
        Integer size = requestVO.getSize();
        Integer id = requestVO.getId();
        String platName = requestVO.getPlatName();
        String startTime = requestVO.getStartTime();
        String endTime = requestVO.getEndTime();

        IPage<SiteReview> page = new Page<>(current,size);
        LambdaQueryWrapper<SiteReview> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(id != null,SiteReview::getId,id)
                .like(StringUtils.isNotBlank(platName),SiteReview::getPlatName,platName)
                .between(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime),SiteReview::getCreateTime,startTime,endTime)
                .orderByDesc(SiteReview::getId);
        page = this.page(page,queryWrapper);

        return new CommonPage<>(page.getRecords(),page.getTotal(),size,current);
    }

    @Override
    public void edit(SiteReview siteReview){
        siteReview.setHandleName(SecurityUtils.getLoginUser().getSysUser().getNickName());
        siteReview.setHandleTime(LocalDateTime.now());
        this.updateById(siteReview);

    }

    @Override
    public List<SiteReviewExportVO> recordListExport(SiteReviewRequestVO requestVO){
        List<SiteReviewExportVO> list = new ArrayList<>();

        String platName = requestVO.getPlatName();
        String startTime = requestVO.getStartTime();
        String endTime = requestVO.getEndTime();
        LambdaQueryWrapper<SiteReview> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(platName),SiteReview::getPlatName,platName)
                .between(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime),SiteReview::getCreateTime,startTime,endTime)
                .orderByDesc(SiteReview::getId);
        List<SiteReview> list1 = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list1)){
            for (int i = 0; i < list1.size(); i++) {
                SiteReviewExportVO exportVO = new SiteReviewExportVO();

                exportVO.setPlatName(list1.get(i).getPlatName());
                exportVO.setConsultName(list1.get(i).getConsultName());
                exportVO.setConsultPhone(list1.get(i).getConsultPhone());
                exportVO.setMail(list1.get(i).getMail());
                exportVO.setCompany(list1.get(i).getCompany());
                exportVO.setConsultContent(list1.get(i).getConsultContent());
                exportVO.setStatus(list1.get(i).getStatus());
                exportVO.setCreateTime(list1.get(i).getCreateTime());
                if(Objects.nonNull(list1.get(i).getHandleName())){
                    exportVO.setHandleName(list1.get(i).getHandleName());
                }
                if(Objects.nonNull(list1.get(i).getHandleContent())){
                    exportVO.setHandleContent(list1.get(i).getHandleContent());
                }
                if(Objects.nonNull(list1.get(i).getHandleTime())){
                    exportVO.setHandleTime(list1.get(i).getHandleTime());
                }
                list.add(exportVO);
            }
        }
        return list;

    }
}




