/*
 * 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.core.dto.Order;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.enums.WxEnum;
import com.tofly.feesapi.udb.entity.User;
import com.tofly.feesapi.udb.service.IUserService;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxRkssq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.dto.WxCommonDto;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.RkstjVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.WxRkssqVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxRkssqService;
import com.tofly.feesapi.yhgl.entity.dto.YhkhjbxxCbbWfpFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.mapper.WxRkssqMapper;
import org.springframework.util.CollectionUtils;

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

/**
 * 微信-人口数变更申请
 *
 * @author sunjian
 * @date Thu Feb 20 00:00:00 CST 2025
 */
@Service("wxRkssqService")
public class WxRkssqServiceImpl extends ServiceImpl<WxRkssqMapper, WxRkssq> implements WxRkssqService {

    @Autowired
    private IUserService userService;

    /**
     * 自定义分页查询
     *
     * @param page
     * @param dto
     * @return
     */
    @Override
    public Page<WxRkssqVo> getWxRkssqPage(Page page, WxCommonDto dto) {
        WxRkssq wxRkssq = JSONObject.parseObject(JSON.toJSONString(dto), WxRkssq.class);
        QueryWrapper<WxRkssq> 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(wxRkssq);
        queryWrapper.orderByDesc("register_date");

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

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

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

            if (null != info.getState()) {
                baseInfo.setStateName(WxEnum.getCodeForValue(info.getState()));
            }
            infoVoList.add(baseInfo);
        });
        return infoVoList;
    }

    /**
     * 人口数统计
     *
     * @param filter
     * @return
     */
    public List<RkstjVo> SelectRkstj(YhkhjbxxCbbWfpFilter filter) {
        return this.baseMapper.SelectRkstj(filter);
    }

    /**
     * 人口数统计
     *
     * @param filter
     * @return
     */
    public PageResult<RkstjVo> getPageRkstj(PageDto pageDto,YhkhjbxxCbbWfpFilter filter) {
        List<Order> orders = pageDto.getOrders();
        orders = orders.stream().map(item -> {
            if (item.getColumn().equals("czsj")) {
                Order order = new Order();
                order.setColumn("a.register_date");
                order.setAsc(item.isAsc());
                return order;
            }
            return item;
        }).collect(Collectors.toList());
        pageDto.setOrders(orders);

        PageDto<RkstjVo> pageRkstj = baseMapper.SelectRkstj(pageDto, filter);
        List<User> userList = userService.getAllUser();
        pageRkstj.getRecords().forEach(e -> {
            if (e.getCby() != null) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(e.getCby()))).findFirst().orElse(null);
                if (user != null) {
                    e.setCbymc(user.getRealName());
                } else {
                    e.setCbymc(e.getCby());
                }
            }
        });
        return pageRkstj.toVo();
    }
}
