package org.dromara.payment.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.enums.BusRole;
import org.dromara.common.core.service.BaseService;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.payment.channel.domain.BusChannelSalesman;
import org.dromara.payment.channel.domain.vo.BusChannelSalesmanVo;
import org.dromara.payment.merchant.domain.vo.BusMerchantVo;
import org.dromara.payment.merchant.mapper.BusMerchantMapper;
import org.dromara.payment.sys.domain.bo.ProInfoCommonBo;
import org.dromara.payment.sys.domain.vo.ProInfoCommonVo;
import org.dromara.payment.sys.domain.vo.SysSydVo;
import org.dromara.payment.sys.mapper.ProInfoCommnoMapper;
import org.dromara.payment.sys.service.IProInfoCommonService;
import org.dromara.system.api.RemoteDictService;
import org.dromara.system.api.domain.vo.RemoteDictDataVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sunzexing
 * @version 1.0
 * @title ProInfoCommonServiceImpl
 * @description
 * @create 2024-04-24 17:36
 */

@Service
public class ProInfoCommonServiceImpl extends BaseService implements IProInfoCommonService {


    @Resource
    private ProInfoCommnoMapper  proInfoCommnoMapper;

    @Resource
    private BusMerchantMapper busMerchantMapper;

    @DubboReference
    private RemoteDictService remoteDictService;
    @Override
    public TableDataInfo<ProInfoCommonVo> queryPageList(ProInfoCommonBo bo, PageQuery pageQuery) {
        QueryWrapper<ProInfoCommonBo> lqw = Wrappers.query();
        lqw.between(bo.getStartTime() != null ,"t.create_time",bo.getStartTime(),bo.getEndTime());
        lqw.like(StringUtils.isNotEmpty(bo.getName()),"t2.gsmc",bo.getName());
        lqw.like(StringUtils.isNotEmpty(bo.getNo()),"t2.no",bo.getNo());
        lqw.like(StringUtils.isNotEmpty(bo.getProNo()),"t1.no",bo.getProNo());
        lqw.eq("t1.pro_status",1);
        lqw.orderByDesc("t.create_time");
        Page<ProInfoCommonVo> result = null;
        int busRole = LoginHelper.getLoginUser().getBusRole();
        if(busRole == BusRole.AGENT.getId()){
            lqw.eq(StringUtils.isNotEmpty(LoginHelper.getNo()),"t.channel_no",LoginHelper.getNo());
            lqw.eq("t.pro_status",1);
            result = this.proInfoCommnoMapper.selectCommonProListAgent( pageQuery.build(), lqw);
        }else if(busRole == BusRole.MERCHANT.getId()){
            lqw.eq(StringUtils.isNotEmpty(LoginHelper.getNo()),"t.mer_no",LoginHelper.getNo());
            lqw.eq("t.pro_status",1);
            result = this.proInfoCommnoMapper.selectCommonProListMer( pageQuery.build(), lqw);
        }else if(busRole == BusRole.OPERATOR.getId()){
            lqw.eq(StringUtils.isNotEmpty(LoginHelper.getNo()),"t.op_no",LoginHelper.getNo());
            lqw.eq("t.pro_status",1);
            result = this.proInfoCommnoMapper.selectCommonProListOper( pageQuery.build(), lqw);
        }else{
            result = new Page<ProInfoCommonVo>();
        }
        return TableDataInfo.build(result);

    }


    /***
     * 获取不同角色业务类型
     * @return
     */
    @Override
    public List<Integer> selDifRoleBusType() {
        List<Integer> result = null;
        QueryWrapper<ProInfoCommonBo> lqw = Wrappers.query();
        lqw.eq("t.pro_status",1);
        int busRole = LoginHelper.getLoginUser().getBusRole();
        if(busRole == BusRole.AGENT.getId()){
            lqw.eq("t1.pro_status",1);
            lqw.eq(StringUtils.isNotEmpty(LoginHelper.getNo()),"t.channel_no",LoginHelper.getNo());
            result =this.proInfoCommnoMapper.selectAgentBusType(lqw);
        }else if(busRole == BusRole.OPERATOR.getId()){
            lqw.eq("t1.pro_status",1);
            lqw.eq(StringUtils.isNotEmpty(LoginHelper.getNo()),"t.op_no",LoginHelper.getNo());
            result =this.proInfoCommnoMapper.selectOperBusType(lqw);
        }else if(busRole == BusRole.SERVICE.getId()){
            lqw.eq(StringUtils.isNotEmpty(LoginHelper.getNo()),"t.syd_id",LoginHelper.getBusId());
            result =this.proInfoCommnoMapper.selectOperBusType(lqw);
        }else if(busRole == BusRole.PLATFORM.getId()){
            result = this.proInfoCommnoMapper.selectOperBusType(lqw);
        }
        return result;
    }

    @Override
    public List<Map> selectAgentService(ProInfoCommonBo commonBo) {
        if(commonBo.getMerchantId() == null){
            this.invalidationParamsException("企业ID不能为空");
        }
        int busRole = LoginHelper.getLoginUser().getBusRole();
        if(busRole == BusRole.AGENT.getId()){
            commonBo.setBusId(LoginHelper.getBusId());
            return this.proInfoCommnoMapper.selectAgentService(commonBo);
        }else if(busRole == BusRole.OPERATOR.getId()){

        }
        return  null;
    }






    public List<BusMerchantVo>   selectMerchantList(ProInfoCommonBo commonBo){

        int busRole = LoginHelper.getBusRole();
        if(busRole == BusRole.SERVICE.getId()){
            return this.proInfoCommnoMapper.selectMerListByServiceId(LoginHelper.getBusId());
        }else{
            QueryWrapper lqw = Wrappers.query();
            lqw.eq("t.verify_status",2);
            lqw.orderByDesc("t.create_time");

            return busMerchantMapper.selectMerchantListByRole(lqw);
        }

    }


    /***
     * 根据企业信息查询对应的服务商
     * @param merchantVo
     * @return
     */
    @Override
    public List<SysSydVo> selectServiceInfoByMerInfo(BusMerchantVo merchantVo) {
        if(merchantVo.getId() == null){
            this.invalidationParamsException("企业ID不能为空");
        }
        return this.proInfoCommnoMapper.selectServiceInfoByMerInfo(merchantVo);
    }


    /***
     * 根据企业信息、服务商信息，查询对应的业务类型
     * @param merId
     * @param sydId
     * @return
     */
    @Override
    public List<Integer> selectBusTypeByMerSerInfo(Long merId, Long sydId) {

        if(merId == null){
            this.invalidationParamsException("企业ID不能为空");
        }

        if(sydId == null){
            this.invalidationParamsException("服务商ID不能为空");
        }

        Map paraMap = new HashMap();
        paraMap.put("merId",merId);
        paraMap.put("sydId",sydId);

        return this.proInfoCommnoMapper.selectBusTypeByMerSerInfo(paraMap);

    }


    @Override
    public  List<RemoteDictDataVo>  selectBusTypeByMerSerInfoName(Long merId, Long sydId){
        List<Integer> busTypeList = this.selectBusTypeByMerSerInfo(merId,sydId);
        List<RemoteDictDataVo>  list =   remoteDictService.selectDictDataByType("wxfe_bus_type");
        List<RemoteDictDataVo>  result = new ArrayList();
        for(Integer bs :busTypeList){
            for(RemoteDictDataVo rd : list){
                if(bs.equals(Integer.valueOf(rd.getDictValue()))){
                    result.add(rd);
                    break;
                }
            }
        }

        return result;

    }
}
