package com.miaxis.extension.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.common.core.domain.entity.SysDictData;
import com.miaxis.extension.domain.WxExtensionIncome;
import com.miaxis.extension.mapper.WxExtensionIncomeMapper;
import com.miaxis.extension.service.IWxExtensionIncomeService;
import com.miaxis.extension.vo.ExtensionIncomeVo;
import com.miaxis.extension.vo.WxGzhExtensionIncomeVo;
import com.miaxis.spread.domain.WxSpreadRelation;
import com.miaxis.spread.service.IWxSpreadRelationService;
import com.miaxis.system.service.ISysDictDataService;
import com.miaxis.system.service.ISysDictTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 分成收益Service业务层处理
 * @author wwl
 * @version 1.0
 * @date 2021/11/4 14:38
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WxExtensionIncomeServiceImpl extends ServiceImpl<WxExtensionIncomeMapper, WxExtensionIncome> implements IWxExtensionIncomeService {


    private final WxExtensionIncomeMapper wxExtensionIncomeMapper;

    private final IWxSpreadRelationService wxSpreadRelationService;

    private final ISysDictTypeService dictTypeService;

    private final ISysDictDataService dictDataService;

    /**
     * 查询分成收益列表
     *
     * @param wxExtensionIncome 分成收益
     * @return 分成收益
     */
    @Override
    public List<ExtensionIncomeVo> selectWxExtensionIncomeList(ExtensionIncomeVo wxExtensionIncome){
        return wxExtensionIncomeMapper.selectWxExtensionIncomeList(wxExtensionIncome);
    }

    /**
     * 新增收益信息
     * @param outTradeNo  商户订单号
     * @param openid 用户openid
     * @param total 订单金额
     * @return
     */
    @Override
    public void addExtensionIncomeBySourceId(String outTradeNo, String openid, Integer total) {




        //查找一二级绑定关系
        WxSpreadRelation oneRelation = wxSpreadRelationService.getOne(new QueryWrapper<WxSpreadRelation>().eq("openid", openid));
        if (oneRelation == null){
            log.info(openid+": 此用户不存在绑定关系");
        }

        WxSpreadRelation twoRelation = wxSpreadRelationService.getOne(new QueryWrapper<WxSpreadRelation>().eq("openid", oneRelation.getParentOpenid()));

        //根据字典获取分成百分比
        List<SysDictData> dictDatas = dictTypeService.selectDictDataByType("sys_divide_percent");

        List<WxExtensionIncome> extensionIncomes = new ArrayList<>();
        dictDatas.forEach(d -> {
            if ("一级".equals(d.getDictLabel())){
                WxExtensionIncome income = new WxExtensionIncome();
                income.setOutTradeNo(outTradeNo);
                income.setBeneficiaryOpenid(oneRelation.getParentOpenid());
                income.setSourceOpenid(openid);
                income.setHierarchy("1");
                income.setPercentage(Integer.parseInt(d.getDictValue()));
                income.setProfitPrice((total*Integer.parseInt(d.getDictValue()))/100);
                extensionIncomes.add(income);
            }
            if ("二级".equals(d.getDictLabel())){
                if (twoRelation != null){
                    WxExtensionIncome income = new WxExtensionIncome();
                    income.setOutTradeNo(outTradeNo);
                    income.setBeneficiaryOpenid(twoRelation.getParentOpenid());
                    income.setSourceOpenid(openid);
                    income.setHierarchy("2");
                    income.setPercentage(Integer.parseInt(d.getDictValue()));
                    income.setProfitPrice((total*Integer.parseInt(d.getDictValue()))/100);
                    extensionIncomes.add(income);
                }
            }

            if ("销售".equals(d.getDictLabel())){
                if (twoRelation != null){
                    //销售提成
                    List<WxSpreadRelation> wxSpreadRelationList = wxSpreadRelationService.getWxSpreadRelationList(twoRelation.getParentOpenid());
                    if (!wxSpreadRelationList.isEmpty()){
                        wxSpreadRelationList.stream().forEach(o->{
                            if (o.getSaleType()!=null && o.getSaleType() > 0){
                                WxExtensionIncome income = new WxExtensionIncome();
                                income.setOutTradeNo(outTradeNo);
                                income.setBeneficiaryOpenid(o.getParentOpenid());
                                income.setSourceOpenid(openid);
                                income.setHierarchy("3");
                                income.setPercentage(Integer.parseInt(d.getDictValue()));
                                income.setProfitPrice((total*Integer.parseInt(d.getDictValue()))/100);
                                extensionIncomes.add(income);
                            }
                        });
                    }
                }
            }
        });

        //保存分成收益信息
        this.saveBatch(extensionIncomes);
    }



    /**
     * 查询收益列表
     * @param openid openid
     * @param nickName 微信昵称
     * @return
     */
    @Override
    public List<WxGzhExtensionIncomeVo> extensionIncomeList(String openid, String nickName) {
        return wxExtensionIncomeMapper.extensionIncomeList(openid,nickName);
    }


    /**
     * 查询收益金额（总金额、已提现金额、未提现金额）
     * @param openid
     * @return
     */
    @Override
    public ExtensionIncomeVo extensionIncomePrice(String openid) {
        return wxExtensionIncomeMapper.extensionIncomePrice(openid);
    }

}
