package io.renren.modules.sys.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.sys.dao.LandingPagesDao;
import io.renren.modules.sys.dao.ScopeCityDao;
import io.renren.modules.sys.dto.ScopeCityDTO;
import io.renren.modules.sys.entity.LandingPagesEntity;
import io.renren.modules.sys.entity.ScopeCityEntity;
import io.renren.modules.sys.service.ScopeCityService;

/**
 * 落地城市主表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2022-05-28
 */
@Service
public class ScopeCityServiceImpl extends CrudServiceImpl<ScopeCityDao, ScopeCityEntity, ScopeCityDTO> implements ScopeCityService {

    @Autowired
    private LandingPagesDao landingPagesDao;
    @Override
    public QueryWrapper<ScopeCityEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String pageId = (String)params.get("pageId");
        String city = (String)params.get("city");
        String strategy = (String)params.get("strategy");

        QueryWrapper<ScopeCityEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id).eq("status",1)
                .eq(StringUtils.isNotBlank(pageId), "page_id", pageId)
                .eq(StringUtils.isNotBlank(city), "city", city)
                .eq(StringUtils.isNotBlank(strategy), "strategy", strategy)
                .orderByDesc("id");

        return wrapper;
    }

    @Override
    public PageData<ScopeCityDTO> page(Map<String, Object> params) {
        IPage<ScopeCityEntity> page = baseDao.selectPage(
                getPage(params, null, false),
                getWrapper(params)
        );
        List<ScopeCityDTO> scopeCityDTOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(page.getRecords())){
            ScopeCityDTO scopeCityDTO = null;
            for (ScopeCityEntity scopeCityEntity:page.getRecords()){
                scopeCityDTO = new ScopeCityDTO();
                BeanUtils.copyProperties(scopeCityEntity,scopeCityDTO);
                LandingPagesEntity landingPagesEntity = landingPagesDao.selectById(scopeCityEntity.getPageId());
                if (landingPagesEntity!= null){
                    scopeCityDTO.setPageName(landingPagesEntity.getName());
                }
                scopeCityDTOList.add(scopeCityDTO);
            }
        }
        return new PageData<>(scopeCityDTOList, page.getTotal());
    }

    @Override
    public List<ScopeCityDTO> list(Map<String, Object> params) {
        List<ScopeCityEntity> entityList = baseDao.selectList(getWrapper(params));
        List<ScopeCityDTO> scopeCityDTOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(entityList)){
            ScopeCityDTO scopeCityDTO = null;
            for (ScopeCityEntity scopeCityEntity:entityList){
                scopeCityDTO = new ScopeCityDTO();
                BeanUtils.copyProperties(scopeCityEntity,scopeCityDTO);
                LandingPagesEntity landingPagesEntity = landingPagesDao.selectById(scopeCityEntity.getPageId());
                if (landingPagesEntity!= null){
                    scopeCityDTO.setPageName(landingPagesEntity.getName());
                }
                scopeCityDTOList.add(scopeCityDTO);
            }
        }
        return scopeCityDTOList;
    }

    @Override
    public ScopeCityDTO get(Long id) {
        ScopeCityEntity scopeCityEntity = baseDao.selectById(id);
        if (scopeCityEntity!=null){
            ScopeCityDTO scopeCityDTO = new ScopeCityDTO();
            BeanUtils.copyProperties(scopeCityEntity,scopeCityDTO);
            LandingPagesEntity landingPagesEntity = landingPagesDao.selectById(scopeCityEntity.getPageId());
            if (landingPagesEntity!= null){
                scopeCityDTO.setPageName(landingPagesEntity.getName());
            }
            return scopeCityDTO;
        }
        return null;
    }
}