package com.miaxis.pc.controller.extension;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miaxis.common.annotation.Log;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.domain.entity.SysDictData;
import com.miaxis.common.core.domain.entity.UserInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.common.utils.poi.ExcelUtil;
import com.miaxis.extension.dto.ExtensionIncomeListDTO;
import com.miaxis.extension.service.IWxExtensionIncomeService;
import com.miaxis.extension.vo.ExtensionIncomeVo;
import com.miaxis.spread.service.IWxSpreadRelationService;
import com.miaxis.system.service.ISysDictTypeService;
import com.miaxis.user.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/11/29 15:58
 */
@RestController
@RequestMapping("/extension/income")
@Api(tags={"【PC-推广积分】"})
@AllArgsConstructor
public class PcWxExtensionIncomeController extends BaseController {

    private final IWxExtensionIncomeService extensionIncomeService;

    private final IUserInfoService userInfoService;

    private final IWxSpreadRelationService wxSpreadRelationService;

    private final ISysDictTypeService dictTypeService;




    /**
     * 查询下级推广积分
     */
    @GetMapping(value = "/extensionPoints")
    @ApiOperation("查询下级推广积分")
    public Response<List<ExtensionIncomeVo>> getDownLevelPoints(ExtensionIncomeListDTO dto) {
        List<ExtensionIncomeVo> list = userInfoService.getPcDownLevelPoints(dto);
        return Response.success(list);
    }



    @GetMapping(value = "/info")
    @ApiOperation("获取代理列表")
    public Response<List<UserInfo>> getInfo(){
        List<String> saleTypeList = new ArrayList<>();
        saleTypeList.add("2");
        saleTypeList.add("3");
        return Response.success(userInfoService.list(new QueryWrapper<UserInfo>().in("sale_type",saleTypeList)));
    }


    /**
     *
     */
    @PreAuthorize("@ss.hasPermi('proxyscore:index:exportExcel')")
    @Log(title = "导出积分列表", businessType = BusinessTypeEnum.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出积分列表")
    public Response<String> export(ExtensionIncomeListDTO dto){
        List<ExtensionIncomeVo> list = userInfoService.getPcDownLevelPoints(dto);
        //根据字典获取分成百分比
        List<SysDictData> dictDatas = dictTypeService.selectDictDataByType("sys_divide_percent");
        Optional<SysDictData> saleOption = dictDatas.stream().filter(o -> o.getDictLabel().equals("销售")).findFirst();
        String value = saleOption.get().getDictValue();
        Double divide = Double.valueOf(value)/100.0;
        list.stream().forEach(o->{
            o.setSettleMoney(o.getAchievement()*divide/100.0);
            o.setDivide(value+"%");
        } );
        ExcelUtil<ExtensionIncomeVo> util = new ExcelUtil<ExtensionIncomeVo>(ExtensionIncomeVo.class);
        return util.exportExcel(list, "code");
    }

    @PreAuthorize("@ss.hasPermi('proxyscore:index:balance')")
    @GetMapping(value = "/extensionIncomePrice/{ids}")
    @ApiOperation("结算积分")
    public Response extensionIncomePrice( @ApiParam(name = "ids", value = "collectionids参数", required = true)
                                              @PathVariable Long[] ids) {



        List<UserInfo> list = userInfoService.list(new QueryWrapper<UserInfo>().in("id", Arrays.asList(ids)));

        list.stream().forEach(userInfo->{
            userInfo.setAchievementSettled(userInfo.getAchievementSettled()+userInfo.getAchievement());
            userInfo.setAchievement(0);
        });
        userInfoService.updateBatchById(list);

        return Response.success();
    }



}
