/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.feesapi.common.enums.WxEnum;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxGhsq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.dto.WxCommonDto;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.WxGhsqVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.mapper.WxGhsqMapper;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxGhsqService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 微信-更名过户申请
 *
 * @author sxb
 * @date Thu Jul 06 00:00:00 CST 2023
 */
@Service("wxGhsqService")
public class WxGhsqServiceImpl extends ServiceImpl<WxGhsqMapper, WxGhsq> implements WxGhsqService {
    /**
     * 自定义分页查询
     *
     * @param page
     * @param dto
     * @return
     */
    @Override
    public Page<WxGhsqVo> getWxGhsqPage(Page page, WxCommonDto dto) {
        WxGhsq wxGhsq = JSONObject.parseObject(JSON.toJSONString(dto), WxGhsq.class);
        QueryWrapper<WxGhsq> queryWrapper = new QueryWrapper<>();
        Date starttime = dto.getStarttime();
        Date endtime = dto.getEndtime();

        String searchText = dto.getSearchText();

        //关键字查询
        if (null != searchText) {
            queryWrapper.or().like("register_yhbh", searchText);
            queryWrapper.or().like("register_name", searchText);
            queryWrapper.or().like("register_address", searchText);
            queryWrapper.or().like("register_phone", searchText);
            dto.setSearchText(null);
        }
        //添加时间条件
        if (null != starttime && null != endtime) {
            queryWrapper.ge("register_date", starttime).lt("register_date", endtime);
            dto.setStarttime(null);
            dto.setEndtime(null);
        } else if (null != starttime) {
            queryWrapper.ge("register_date", starttime);
            dto.setStarttime(null);
        } else if (null != endtime) {
            queryWrapper.le("register_date", endtime);
            dto.setEndtime(null);
        }

        queryWrapper.setEntity(wxGhsq);
        queryWrapper.orderByDesc("register_date");

        Page infoPageList = this.page(page, queryWrapper);
        infoPageList.setRecords(this.viewConversion(infoPageList.getRecords()));
        return infoPageList;
    }

    /**
     * 转换视图
     *
     * @param infoList
     * @return
     */
    public List<WxGhsqVo> viewConversion(List<WxGhsq> infoList) {
        if (CollectionUtils.isEmpty(infoList)) {
            return Collections.emptyList();
        }

        List<WxGhsqVo> infoVoList = new ArrayList<>();
        infoList.forEach(info -> {
            WxGhsqVo baseInfo = JSONObject.parseObject(JSON.toJSONString(info), WxGhsqVo.class);

            if (null != info.getState()) {
                baseInfo.setStateName(WxEnum.getCodeForValue(info.getState()));
            }
            if (null != info.getType()) {
                baseInfo.setTypeName(WxEnum.getCodeForValue(info.getType() + "1"));
            }
            infoVoList.add(baseInfo);
        });
        return infoVoList;
    }
}
