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

import cn.com.countrygarden.bi.main.dao.extension.ExtensionReportMapper;
import cn.com.countrygarden.bi.main.dao.extension.ExtensionErpBindingMapper;
import cn.com.countrygarden.bi.main.constant.ExtensionContent;
import cn.com.countrygarden.bi.main.dto.extension.ExtensionErpDTO;
import cn.com.countrygarden.bi.main.dto.extension.ExtensionPageData;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.extension.ExtensionErpBindingService;
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 市场拓展 -> 外拓绑定 -> ERP 绑定业务逻辑 --> Impl
 * @Author Sjiang
 * @Date 2019/1/15 11:17
 * @Version 1.0
 **/
@Service
@Transactional
public class ExtensionErpBindingServiceImpl implements ExtensionErpBindingService {

    @Autowired
    private ExtensionErpBindingMapper mapper;
    @Autowired
    private ExtensionReportMapper reportMapper;
    @Autowired
    private ExtensionReportService reportService;

    /**
     * @return PageData
     * @Author Sjiang
     * @Description // 根据 bindingIdentifier 查询对应状态的信息
     * @Date 11:23 2019/1/17
     * @Param [dto, pageRowBounds]
     **/
    @Override
    public Object selectErpBindingOrUnbinding(ExtensionErpDTO dto, AccessTokenService accessTokenService, HttpServletRequest request, ExtensionPageData pageData) {
        ExtensionReportUtils.checkBindingIdentifier(dto);
        String organName = getOrganCode(accessTokenService, request);
        List<ExtensionErpDTO> extensionErpDTOList = null;
        PageRowBounds pageRowBounds = new PageRowBounds(pageData.getPageIndex(), pageData.getPageSize());
        // 查询 未绑定
        if (ExtensionContent.BINDING_IDENTIFIER_UN.equals(dto.getBindingIdentifier())) {
            // 先查已绑定的 ids 然后排除查询
            // 获取 ID 集合
            List<Long> ids = reportMapper.selectErpBindingId(organName);

            extensionErpDTOList = mapper.selectErpUnbinding(ids,organName, dto, pageRowBounds);

        }
        // 查询已绑定
        else {
            extensionErpDTOList = reportMapper.selectErpBindingList(dto,organName, pageRowBounds);
        }
        return new ExtensionPageData(extensionErpDTOList, pageRowBounds);
    }

    /**
     * @Author sjiang
     * @Description  加入区域权限控制的查询
     * @Date 11:00 2019-03-16
     * @Param [reportDTO, accessTokenService, request, pageData]
     * @return java.lang.Object
     **/
    @Override
    public Object selectAll(ExtensionErpDTO erpDTO, AccessTokenService accessTokenService, HttpServletRequest request, ExtensionPageData pageData) {
        ExtensionReportUtils.checkBindingIdentifier(erpDTO);
        String organCode = getOrganCode(accessTokenService, request);
        PageRowBounds pageRowBounds = new PageRowBounds(pageData.getPageIndex(),pageData.getPageSize());
        List<ExtensionErpDTO> plist = mapper.selectAllByDTO(erpDTO,organCode, pageRowBounds);
        return new ExtensionPageData(plist, pageRowBounds);
    }

    private String getOrganCode(AccessTokenService accessTokenService, HttpServletRequest request){
        // 获取 userCode
        String userCode = ExtensionReportUtils.getUserCode(accessTokenService, request);
        // 获取 organCode ，如果为空则是总部权限
        return reportService.isExtensionAdmin(userCode);
    }
}
