package cn.com.countrygarden.bi.main.service.extension.impl;


import cn.com.countrygarden.bi.main.dao.extension.ExtensionDupBindingMapper;
import cn.com.countrygarden.bi.main.dto.extension.ExtensionDupDTO;
import cn.com.countrygarden.bi.main.dto.extension.ExtensionPageData;
import cn.com.countrygarden.bi.main.dto.extension.ExtensionReportDTO;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.extension.ExtensionDupBindingService;
import cn.com.countrygarden.bi.main.service.extension.ExtensionReportService;
import cn.com.countrygarden.bi.main.util.ExtensionReportUtils;
import com.github.pagehelper.PageRowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName ExtensionBindingServieImpl
 * @Description 市场拓展 -> 外拓绑定 -> DUP 绑定业务逻辑 --> Impl
 * @Author Sjiang
 * @Date 2019/1/15 11:17
 * @Version 1.0
 **/
@Service
@Transactional
public class ExtensionDupBindingServiceImpl implements ExtensionDupBindingService {

    @Autowired
    private ExtensionDupBindingMapper mapper;
    @Autowired
    private ExtensionReportService reportService;

    @Override
    public Object selectAll(ExtensionDupDTO dupDTO, AccessTokenService accessTokenService, HttpServletRequest request, ExtensionPageData pageData) {
        ExtensionReportUtils.checkBindingIdentifier(dupDTO);
        // 获取 userCode
        String userCode = ExtensionReportUtils.getUserCode(accessTokenService, request);
        // 获取 organCode ，如果为空则是总部权限
        String organCode = reportService.isExtensionAdmin(userCode);
        PageRowBounds pageRowBounds = new PageRowBounds(pageData.getPageIndex(),pageData.getPageSize());
        List<ExtensionDupDTO> plist = mapper.selectAllByDTO(dupDTO,organCode, pageRowBounds);
        return new ExtensionPageData(plist, pageRowBounds);
    }
}