package com.aircraft.modules.system.service.impl;

import com.aircraft.modules.system.domain.EmArea;
import com.aircraft.modules.system.domain.EmEmployees;
import com.aircraft.modules.system.domain.EmScenic;
import com.aircraft.modules.system.domain.User;
import com.aircraft.modules.system.domain.vo.AreaNumStatisVo;
import com.aircraft.modules.system.mapper.EmScenicMapper;
import com.aircraft.modules.system.service.EmAreaService;
import com.aircraft.modules.system.service.EmEmployeesService;
import com.aircraft.modules.system.service.EmScenicService;
import com.aircraft.modules.system.service.UserService;
import com.aircraft.utils.PageResult;
import com.aircraft.utils.PageUtil;
import com.aircraft.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cli
 * @since 2025-07-09
 */
@Service
public class EmScenicServiceImpl extends ServiceImpl<EmScenicMapper, EmScenic> implements EmScenicService {

    @Lazy
    @Autowired
    private EmAreaService areaService;
    @Lazy
    @Autowired
    private UserService userService;

    @Override
    public List<EmScenic> list(EmScenic example) {
        return this.list(buildWrapper(example));
    }

    @Override
    public PageResult<EmScenic> page(EmScenic example, IPage page) {
        IPage<EmScenic> emScenicIPage = this.page(page,buildWrapper(example));
        if (emScenicIPage.getRecords() != null&& !emScenicIPage.getRecords().isEmpty()) {
            for (EmScenic record : emScenicIPage.getRecords()) {
                List<User> fdBaseUsers = userService.findByScenicId(record.getId());
                record.setPeopleNum(fdBaseUsers.size());
                record.setAreaName(areaService.findById(record.getAreaId()).getName());
            }
        }
        return PageUtil.toPage(emScenicIPage.getRecords());
    }

    @Override
    public List<EmScenic> findByAreaId(Long areaId) {
        QueryWrapper<EmScenic> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(EmScenic::getAreaId, areaId)
                .eq(EmScenic::getDelFlag, 0);
        return super.list(wrapper);
    }

    @Override
    public EmScenic findByAreaIdAndName(Long areaId, String name) {
        return this.baseMapper.findByAreaIdAndName(areaId, name);
    }

    @Override
    public void countByAreaIdToUpdate() {
        areaService.setAreaScenicNumToZero();
        List<AreaNumStatisVo> statis          = this.baseMapper.countByAreaId();
        Map<Integer, Integer> organizationMap = statis.stream().collect(Collectors.toMap(AreaNumStatisVo::getAreaId, AreaNumStatisVo::getScenicNum));
        List<EmArea>      organizations   = areaService.findByIdIn(statis.stream().map(AreaNumStatisVo::getAreaId).collect(Collectors.toList()));
        for (EmArea organization : organizations) {
            organization.setScenicNum(organizationMap.get(organization.getId()));
        }
        areaService.updateBatchById(organizations);
    }

    @Override
    public EmScenic obtainScenicById(Long id) {
        return this.getById(id);
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<EmScenic> buildWrapper(EmScenic example) {
        QueryWrapper<EmScenic> wrapper = new QueryWrapper<>();

        Long areaId = example.getAreaId();
        String  name           = example.getName();
        String createBy      = example.getCreateBy();

        wrapper.lambda()
                .eq(null != areaId, EmScenic::getAreaId, areaId)
                .like(StringUtils.isNotEmpty(name), EmScenic::getName, name)
                .eq(StringUtils.isNotEmpty(createBy), EmScenic::getCreateBy, createBy)
                .eq(EmScenic::getDelFlag, 0);

        return wrapper;
    }

}
