package com.hzs.member.marketnews.controller.api;

import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EOrderType;
import com.hzs.common.core.enums.EPlaceDept;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.marketnews.param.MarketNewsParam;
import com.hzs.member.marketnews.vo.UserInvestmentVo;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import com.hzs.system.config.IGradeServiceApi;
import com.hzs.system.config.dto.GradeDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/market-news")
public class MarketNewsController extends BaseController {

    @Autowired
    private ICuMemberService memberService;

    @DubboReference
    IGradeServiceApi gradeServiceApi;
    @DubboReference
    ICurrencyServiceApi currencyServiceApi;

    /**
     * 招商列表
     *
     * @return
     */
    @GetMapping("/bidding-list")
    public TableDataInfo biddingList(MarketNewsParam marketNewsParam) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        R<List<GradeDTO>> gradeDate = gradeServiceApi.queryGradeList(pkCountry);
        List<GradeDTO> gradeList = gradeDate.getData();

        marketNewsParam.setPkCountry(pkCountry);
        startPage();
        List<UserInvestmentVo> list = memberService.selectBiddingList(marketNewsParam);
        for (UserInvestmentVo userInvestmentVo : list) {
            if (userInvestmentVo.getOrderType() != null) {
                String label = EOrderType.getEnumByValue(userInvestmentVo.getOrderType()).getLabel();
                userInvestmentVo.setOrderTypeStr(label);
            }
            if (userInvestmentVo.getRound() == null) {
                userInvestmentVo.setRound(0);
            }
            if (userInvestmentVo.getSecond() == null) {
                userInvestmentVo.setSecond(0);
            }

            //注册等级key
            if (userInvestmentVo.getPkRegisterGrade() != null) {
                for (GradeDTO gradeDTO : gradeList) {
                    if (gradeDTO.getPkId().equals(userInvestmentVo.getPkRegisterGrade())) {
                        userInvestmentVo.setRegisterGradeVal(gradeDTO.getGradeName());
                    }
                }
            }
        }
        return getDataTable(list);
    }


    /**
     * 注册列表
     *
     * @return
     */
    @GetMapping("/reg-list")
    public TableDataInfo regList(MarketNewsParam marketNewsParam) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        marketNewsParam.setPkCountry(pkCountry);
        marketNewsParam.setPkCreator(SecurityUtils.getUserId());
        R<List<GradeDTO>> gradeDate = gradeServiceApi.queryGradeList(pkCountry);
        List<GradeDTO> gradeList = gradeDate.getData();
        R<CurrencyDTO> currencyDto = currencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
        CurrencyDTO currency = currencyDto.getData();
        if (marketNewsParam.getEndCreationTime() != null) {
            marketNewsParam.setEndCreationTime(DateUtils.getEndTime(marketNewsParam.getEndCreationTime()));
        }
        startPage();
        List<UserInvestmentVo> list = memberService.selectRegList(marketNewsParam);
        for (UserInvestmentVo userInvestmentVo : list) {
            if (userInvestmentVo.getPkPlaceParent() != null && userInvestmentVo.getPkParent() != null) {
                if (userInvestmentVo.getPkPlaceParent() == 0 || userInvestmentVo.getPkParent() == 0) {
                    userInvestmentVo.setParentName("-");
                    userInvestmentVo.setParentCode("-");
                    userInvestmentVo.setPlaceParentCode("-");
                    userInvestmentVo.setPlaceParentName("-");
                }
            }

            //注册等级key
            if (userInvestmentVo.getPkRegisterGrade() != null) {
                for (GradeDTO gradeDTO : gradeList) {
                    if (gradeDTO.getPkId().equals(userInvestmentVo.getPkRegisterGrade())) {
                        userInvestmentVo.setRegisterGradeVal(gradeDTO.getGradeName());
                    }
                }
            }
            if (userInvestmentVo.getPlaceDept() != null) {
                userInvestmentVo.setPlaceDeptVal(EPlaceDept.getEPlaceDept(userInvestmentVo.getPlaceDept()));
            }
            // 所属国是中国
            if (pkCountry.equals(CountryConstants.CHINA_COUNTRY)) {
                userInvestmentVo.setRegPv(userInvestmentVo.getRegPv().multiply(currency.getInExchangeRate()));
            }

            userInvestmentVo.setMemberCode(userInvestmentVo.getMemberCode());
            userInvestmentVo.setMemberName(userInvestmentVo.getMemberName());
            userInvestmentVo.setParentCode(userInvestmentVo.getParentCode());
            userInvestmentVo.setParentName(userInvestmentVo.getParentName());
            userInvestmentVo.setPlaceParentCode(userInvestmentVo.getPlaceParentCode());
            userInvestmentVo.setPlaceParentName(userInvestmentVo.getPlaceParentName());
        }
        return getDataTable(list);
    }

}
