package com.goafanti.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.goafanti.common.dao.TOrderNewMapper;
import com.goafanti.common.dao.TOrderTaskMapper;
import com.goafanti.common.dao.TTaskMemberMapper;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.bo.InputTechUserListBo;
import com.goafanti.order.bo.TOrderTaskBo;
import com.goafanti.order.service.OrderPlusService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderPlusServiceImpl extends BaseMybatisDao<TOrderNewMapper> implements OrderPlusService {

    @Resource
    private TOrderNewMapper tOrderNewMapper;
    @Resource
    private TOrderTaskMapper tOrderTaskMapper;
    @Resource
    private TTaskMemberMapper tTaskMemberMapper;

    @Override
    public Object techUserList(InputTechUserListBo in) {
        String aid = TokenManager.getAdminId();
        in.setAid(aid);
        return findPage("selectTechUserList","selectTechUserCount",in);
    }

    @Override
    public Object techUserDetails(String uid) {
        List<Map<String, Object>> maps = tOrderNewMapper.selectOrderByuid(uid);
        for (Map<String, Object> map : maps) {
            String contractNo = (String) map.get("contractNo");
            BigDecimal totalAmount = (BigDecimal) map.get("totalAmount");
            String orderNo = (String) map.get("orderNo");
            List<TOrderTaskBo> tOrderTaskBos = tOrderTaskMapper.selectOrderTaskAll(orderNo, null);
            String serviceLife = null;
            List<String> list=new ArrayList<>();
            int  yearSum=0;
            String isHY="";
            List<String> taskList=new ArrayList<>();
            String lvl=null;
            if (totalAmount.compareTo(new BigDecimal("3"))<0){
                lvl="A";
            }else if (totalAmount.compareTo(new BigDecimal("8"))<1){
                lvl="AA";
            }else if (totalAmount.compareTo(new BigDecimal("8"))>0){
                lvl="AAA";
            }

            List<TOrderTaskBo> collect = tOrderTaskBos.stream().filter(e -> e.getSplitStatus() == 1).collect(Collectors.toList());
            for (TOrderTaskBo e : collect) {
                if (e.getYearSum()>yearSum){
                    yearSum=e.getYearSum();
                    serviceLife=e.getServiceLife();
                }
                if(e.getcSort()!=null&&e.getcSort()==6){
                    isHY="是";
                }
            }
            if (serviceLife!=null){
                list = JSON.parseArray(serviceLife, String.class);
            }
            List<TOrderTaskBo> collect2 = tOrderTaskBos.stream().filter(e -> e.getSplitStatus() == 2).collect(Collectors.toList());
            for (TOrderTaskBo e : collect2) {
                for (String s : list) {
                    if (s.equals(e.getServiceYear())){
                        s=s+" (已派)";
                    }
                    taskList.add(s);
                }
            }
            System.out.println("contractNo = " + contractNo);
            System.out.println(taskList);
            System.out.println("会员="+isHY);
            System.out.println("会员等级="+lvl);
            System.out.println("金额="+totalAmount);
        }
        return null;
    }
}
