package org.linlinjava.jz.db.service;

import com.google.common.collect.Lists;
import org.linlinjava.jz.db.dao.JzCampaignAssignedMapper;
import org.linlinjava.jz.db.domain.JzCampaignAssigned;
import org.linlinjava.jz.db.domain.JzCampaignAssignedExample;
import org.linlinjava.jz.db.domain.JzSkillGroupUser;
import org.linlinjava.jz.db.domain.SysUser;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.plaf.PanelUI;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class JzCampaignAssignedService {

    private final JzCampaignAssigned.Column[] result = new JzCampaignAssigned.Column[]{JzCampaignAssigned.Column.id,
            JzCampaignAssigned.Column.shops,
            JzCampaignAssigned.Column.skills,
            JzCampaignAssigned.Column.title,
            JzCampaignAssigned.Column.createTime};

    @Resource
    private JzCampaignAssignedMapper assignedMapper;

    @Resource
    private JzSkillGroupUserService skillGroupUserService;



    public List<JzCampaignAssigned> list(Integer campaignId) {
        JzCampaignAssignedExample example = new JzCampaignAssignedExample();
        example.or().andDeletedEqualTo(false).andCampaignIdEqualTo(campaignId);

        return assignedMapper.selectByExampleSelective(example, result);
    }

    public JzCampaignAssigned findByCampaignId(int campaignId) {
        JzCampaignAssignedExample example = new JzCampaignAssignedExample();
        example.or().andDeletedEqualTo(false).andCampaignIdEqualTo(campaignId);

        return assignedMapper.selectOneByExample(example);
    }

    public JzCampaignAssigned findById(int id) {
        return assignedMapper.selectByPrimaryKey(id);
    }

    /**
     * 分配到技能组
     *
     * @param campaignId
     * @return
     */
    public List<JzCampaignAssigned> listSkillGroup(Integer campaignId) {
        JzCampaignAssignedExample example = new JzCampaignAssignedExample();
        example.or().andDeletedEqualTo(false).andCampaignIdEqualTo(campaignId).andSkillsIsNotNull();

        return assignedMapper.selectByExampleSelective(example, result);
    }

    public void add(JzCampaignAssigned jzCampaignAssigned) {
        jzCampaignAssigned.setUpdateTime(LocalDateTime.now());
        assignedMapper.insertSelective(jzCampaignAssigned);
    }

    public void update(JzCampaignAssigned jzCampaignAssigned) {
        jzCampaignAssigned.setUpdateTime(LocalDateTime.now());
        assignedMapper.updateByPrimaryKeySelective(jzCampaignAssigned);
    }

    //获取已分配的门店 如果指定assignedId，则修改状态 排除
    public List<Integer> queryShopsByCampaignId(int campaignId, Integer assignedId) {
        List<Integer> shops = Lists.newArrayList();

        JzCampaignAssignedExample example = new JzCampaignAssignedExample();
        if (assignedId == null || assignedId == 0)
            example.or().andDeletedEqualTo(false).andCampaignIdEqualTo(campaignId);
        else
            example.or().andDeletedEqualTo(false).andCampaignIdEqualTo(campaignId).andIdNotEqualTo(assignedId);

        List<JzCampaignAssigned> assigneds = assignedMapper.selectByExampleSelective(example, result);
        if (assigneds != null) {
            for (JzCampaignAssigned assigned : assigneds) {
                if (assigned.getShops() != null)
                    shops.addAll(Arrays.asList(assigned.getShops()));
            }
        }

        return shops;
    }

    public List<Integer> querySkillsByCampaignId(int campaignId, Integer assignedId) {
        List<Integer> skills = Lists.newArrayList();

        JzCampaignAssignedExample example = new JzCampaignAssignedExample();
        if (assignedId == null || assignedId == 0)
            example.or().andDeletedEqualTo(false).andCampaignIdEqualTo(campaignId);
        else
            example.or().andDeletedEqualTo(false).andCampaignIdEqualTo(campaignId).andIdNotEqualTo(assignedId);

        List<JzCampaignAssigned> assigneds = assignedMapper.selectByExampleSelective(example, result);
        if (assigneds != null) {
            for (JzCampaignAssigned assigned : assigneds) {
                if (assigned.getSkills() != null)
                    skills.addAll(Arrays.asList(assigned.getSkills()));
            }
        }

        return skills;
    }

}
