package org.linlinjava.jz.admin.service;

import com.google.common.collect.Lists;
import org.linlinjava.jz.admin.vo.ShopVo;
import org.linlinjava.jz.core.util.ResponseUtil;
import org.linlinjava.jz.db.domain.JzCampaignAssigned;
import org.linlinjava.jz.db.domain.JzSkillGroup;
import org.linlinjava.jz.db.domain.JzSkillGroupUser;
import org.linlinjava.jz.db.service.JzCampaignAssignedService;
import org.linlinjava.jz.db.service.JzSkillGroupService;
import org.linlinjava.jz.db.service.JzSkillGroupUserService;
import org.linlinjava.jz.db.service.JzSysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务分配处理类
 */
@Service
public class AdminCamapaignAssignedService {

    @Autowired
    private JzCampaignAssignedService assignedService;

    @Autowired
    private JzSkillGroupUserService skillGroupUserService;

    @Autowired
    private AdminSkillGroupService adminSkillGroupService;

    @Autowired
    private JzCampaignAssignedService campaignAssignedService;

    @Autowired
    private AdminShopService adminShopService;


    @Autowired
    private JzSkillGroupService skillGroupService;

    @Autowired
    private JzSysUserService sysUserService;


    /**
     * 获取此任务下的所有负责坐席 （到人，如果是技能组 转换到用户）
     * @param campaignId
     * @return
     */
    public List<Integer> listUsers(int campaignId){
        List<JzCampaignAssigned> assigneds=assignedService.list(campaignId);

        List<Integer> listUsers = Lists.newArrayList();
        for(JzCampaignAssigned campaignAssigned:assigneds){
            //分配到坐席
           /* if(campaignAssigned.getUsers()!=null){
                for(int user:campaignAssigned.getUsers()){
                    if(!listUsers.contains(user))
                        listUsers.add(user);
                }
            }*/
            //技能组
            if(campaignAssigned.getSkills()!=null){
                List<JzSkillGroupUser> groupUsers=skillGroupUserService.findByGroupId(campaignAssigned.getSkills());
                for(JzSkillGroupUser user:groupUsers)
                    if(!listUsers.contains(user.getUserId()))
                        listUsers.add(user.getUserId());
            }

        }

        return  listUsers;
    }

    /**
     * 获取此任务下所有门店
     * @param campaignId
     * @return
     */
    public List<Integer> listShops(int campaignId){
        List<JzCampaignAssigned> assigneds=assignedService.list(campaignId);

        List<Integer> listShops = Lists.newArrayList();
        for(JzCampaignAssigned campaignAssigned:assigneds) {
            if(campaignAssigned.getShops()!=null){
                for(int shop:campaignAssigned.getShops()){
                    if(!listShops.contains(shop))
                        listShops.add(shop);
                }
            }

        }

        return listShops;
    }

    public Object getAllShopsAndSkills(Integer campaignId,Integer assignId) {
        //获取此任务下 已经分配的门店
        List<Integer> existShops = campaignAssignedService.queryShopsByCampaignId(campaignId, assignId);
        List<ShopVo> shopVos = adminShopService.getShops(existShops);

        //技能组
        List<Integer> existsSkill=campaignAssignedService.querySkillsByCampaignId(campaignId, assignId);
        List<JzSkillGroup> skillGroupList=skillGroupService.queryAll(existsSkill);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("shops", shopVos);
        map.put("skills", skillGroupList);
        return ResponseUtil.ok(map);
    }

    public List<JzCampaignAssigned> list(int campaignId){
        return campaignAssignedService.list(campaignId);
    }


}
