package com.huiju.estateagents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.entity.TaPersonBuilding;
import com.huiju.estateagents.entity.TaShareCount;
import com.huiju.estateagents.excel.SalesExport;
import com.huiju.estateagents.mapper.TaPersonBuildingMapper;
import com.huiju.estateagents.mapper.TaPersonMapper;
import com.huiju.estateagents.mapper.TaShareCountMapper;
import com.huiju.estateagents.mapper.TaSharePersonFromMapper;
import com.huiju.estateagents.service.ITaPersonBuildingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 人员授权项目 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-05-10
 */
@Service
public class TaPersonBuildingServiceImpl extends ServiceImpl<TaPersonBuildingMapper, TaPersonBuilding> implements ITaPersonBuildingService {

    @Autowired
    private TaPersonBuildingMapper taPersonBuildingMapper;

    @Autowired
    private TaShareCountMapper taShareCountMapper;

    @Autowired
    private TaPersonMapper taPersonMapper;

    @Autowired
    private TaSharePersonFromMapper taSharePersonFromMapper;

    @Override
    public ResponseBean selectList(Integer pageNum, Integer pageSize, Integer orgId, String buildingId, String startCreateDate, String endCreateDate, List<TaPersonBuilding> taPersonBuildingList) {
        Page page = new Page(pageNum,pageSize);
        // 分页查询
        List<TaPersonBuilding> list = taPersonBuildingMapper.selectListByParams(page, orgId, buildingId, startCreateDate, endCreateDate, taPersonBuildingList);
        for (TaPersonBuilding taPersonBuilding : list) {
            QueryWrapper<TaPerson> taPersonQueryWrapper = new QueryWrapper<>();
            if (StringUtils.isEmpty(taPersonBuilding.getPersonId())){
                taPersonBuilding.setClickNum(0);
                taPersonBuilding.setShareCount(0);
                continue;
            }
            taPersonQueryWrapper.eq("person_id", taPersonBuilding.getPersonId());
            TaPerson taPerson = taPersonMapper.selectOne(taPersonQueryWrapper);
            taPersonBuilding.setShareCount(taShareCountMapper.countShare(taPersonBuilding.getPersonId(), orgId, startCreateDate, endCreateDate));
            taPersonBuilding.setClickNum(taPerson != null ? taSharePersonFromMapper.countViewNum(taPerson.getUserId(), taPerson.getOrgId(), taPerson.getPersonId(), startCreateDate, endCreateDate) : 0);
        }
        page.setRecords(list);
        return ResponseBean.success(page);
    }

    /**
     * 导出
     *
     * @param buildingId
     * @param startCreateDate
     * @param endCreateDate
     * @param taPersonBuildingList
     * @return
     */
    @Override
    public List<SalesExport> selectSalesList(Integer orgId, String buildingId, String startCreateDate, String endCreateDate, List<TaPersonBuilding> taPersonBuildingList) {
        List<SalesExport> list = taPersonBuildingMapper.selectSalesList(orgId, buildingId, startCreateDate, endCreateDate, taPersonBuildingList);
        for (SalesExport salesExport : list) {
            if (StringUtils.isEmpty(salesExport.getPersonId())){
                salesExport.setClickNum(0);
                continue;
            }

            salesExport.setClickNum(taSharePersonFromMapper.countViewNum(salesExport.getUserId(), orgId, salesExport.getPersonId(), startCreateDate, endCreateDate));
        }
        return list;
    }
}
