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

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

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

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

import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.sys.dao.ChannelsDao;
import io.renren.modules.sys.dto.ChannelsDTO;
import io.renren.modules.sys.entity.ChannelsEntity;
import io.renren.modules.sys.entity.OperationsEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.ChannelsService;
import io.renren.modules.sys.service.OperationsService;
import io.renren.modules.sys.service.SysUserService;

/**
 * 投放渠道表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2022-05-29
 */
@Service
public class ChannelsServiceImpl extends CrudServiceImpl<ChannelsDao, ChannelsEntity, ChannelsDTO> implements ChannelsService {

	@Autowired
	protected SysUserService sysUserService;
	
    @Autowired
    private OperationsService operationsService;
	
    @Override
    public QueryWrapper<ChannelsEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<ChannelsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id).eq("status",1);

        return wrapper;
    }
    
    @Override
    public PageData<ChannelsDTO> page(Map<String, Object> params){
    	IPage<ChannelsEntity> page = baseDao.selectPage(
                getPage(params, null, false),
                getWrapper(params)
        );
    	
    	List<ChannelsDTO> channelsDTOList = new ArrayList<>();
    	
    	for (ChannelsEntity entity:page.getRecords()) {
    		ChannelsDTO dto = new ChannelsDTO();
    		BeanUtils.copyProperties(entity,dto);
    		SysUserEntity creator = sysUserService.selectById(entity.getCreator());
        	if(null != creator) {
        		dto.setCreatorName(creator.getRealName());
        	}
        	
        	SysUserEntity update = sysUserService.selectById(entity.getUpdateBy());
        	if(null != update) {
        		dto.setUpdateName(update.getRealName());
        	}
        	
        	StringJoiner operationNames = new StringJoiner(",");
        	String[] operationIds = entity.getOperationIds().split(",");
        	for (String operationId : operationIds) {
        		OperationsEntity operation = operationsService.selectById(operationId);
        		if(null != operation) {
        			operationNames.add(operation.getName());
        		}
			}
        	dto.setOperationNames(operationNames.toString());
        	channelsDTOList.add(dto);
    	}
    	
    	return new PageData<>(channelsDTOList, page.getTotal());
    }

	@Override
	public List<ChannelsEntity> listAll() {
		return baseDao.selectList(new QueryWrapper<ChannelsEntity>().eq("status", Constant.Status.ENABLE.getValue()));
	}


}