package org.linlinjava.jz.admin.web;

import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.jz.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.jz.admin.service.AdminCamapaignAssignedService;
import org.linlinjava.jz.admin.util.SecurityHelper;
import org.linlinjava.jz.admin.vo.ShopVo;
import org.linlinjava.jz.core.util.JacksonUtil;
import org.linlinjava.jz.core.util.ResponseUtil;
import org.linlinjava.jz.core.validator.Order;
import org.linlinjava.jz.core.validator.Sort;
import org.linlinjava.jz.db.domain.JzCampaign;
import org.linlinjava.jz.db.domain.JzCampaignAssigned;
import org.linlinjava.jz.db.service.JzCampaignAssignedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

@Api(value = "Admin-campaignAssigned-Controller",description = "任务分配")
@RestController
@RequestMapping("/admin/campaignAssigned")
@Validated
public class AdminCampaignAssignedController {

    @Autowired
    private JzCampaignAssignedService campaignAssignedService;

    @Autowired
    private AdminCamapaignAssignedService adminCamapaignAssignedService;

    @RequiresPermissions("admin:campaignAssigned:list")
    @RequiresPermissionsDesc(menu = {"会员营销", "任务分配"}, button = "查询")
    @GetMapping("/list")
    public Object list(Integer campaignId) {

        List<JzCampaignAssigned> campaignList = campaignAssignedService.list(campaignId);
        return ResponseUtil.ok(campaignList);
    }

    /**
     * 已分配的成员---分配到人
     *
     */
    @RequiresPermissions("admin:campaignAssigned:readAssigned")
    @RequiresPermissionsDesc(menu = {"会员营销", "任务分配"}, button = "查看")
    @GetMapping("/readAssigned")
    public Object getUsers(Integer id) {
        //获取系统所有操作用户信息，二级结构：部门-》成员
        //List<SkillGroupVo> allUsers = adminSkillGroupService.getSysUser();
        JzCampaignAssigned campaignAssigned=campaignAssignedService.findById(id);


        //获取已分配的坐席
      /*  Set<String> assignedUser = new HashSet<String>();
        if(campaignAssigned.getUsers()!=null&&campaignAssigned.getUsers().length>0){
            for(Integer value:campaignAssigned.getUsers()){
                assignedUser.add(value.toString());
            }
        }*/

        //获取已分配技能组
        List<Integer> assignedSkill = Lists.newArrayList();
        if(campaignAssigned.getSkills()!=null&&campaignAssigned.getSkills().length>0){
            assignedSkill=Arrays.asList(campaignAssigned.getSkills());
        }

        //已分配门店
        Set<String> assigneShops = new HashSet<String>();
        if(campaignAssigned.getShops()!=null&&campaignAssigned.getShops().length>0){
            for(Integer value:campaignAssigned.getShops()){
                assigneShops.add(value.toString());
            }
        }

        Map<String, Object> data = new HashMap<>();
        data.put("title", campaignAssigned.getTitle());
        data.put("campaignId", campaignAssigned.getCampaignId());
        data.put("assignedShops", assigneShops );
        //data.put("assignedUsers", assignedUser);
        data.put("assignedGroups", assignedSkill);
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:campaignAssigned:deleted")
    @RequiresPermissionsDesc(menu = {"会员营销", "任务分配"}, button = "停用")
    @PostMapping("/delete")
    public Object delete(@RequestBody JzCampaignAssigned campaignAssigned) {
        campaignAssigned = campaignAssignedService.findById(campaignAssigned.getId());
        if (campaignAssigned == null)
            return ResponseUtil.badArgument();

        campaignAssigned.setDeleted(true);
        campaignAssigned.setUpdateUser(SecurityHelper.getAdminId());
        campaignAssigned.setUpdateTime(LocalDateTime.now());

        campaignAssignedService.update(campaignAssigned);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:campaignAssigned:assigned")
    @RequiresPermissionsDesc(menu = {"会员营销", "任务分配"}, button = "任务分配")
    @PostMapping("/assigned")
    public Object assigned(@RequestBody String body) {

        List<Integer> assignedSkillGroup = JacksonUtil.parseIntegerList(body, "assignedSkillGroup");
        List<Integer> assignedShops = JacksonUtil.parseIntegerList(body, "assignedShops");
        Integer campaignId = JacksonUtil.parseInteger(body, "assignedCampaignId");
        String title = JacksonUtil.parseString(body, "title");
        Integer id = JacksonUtil.parseInteger(body, "id");

        if (((assignedSkillGroup == null || assignedSkillGroup.size() == 0)) || (campaignId == null || campaignId == 0))
            return ResponseUtil.badArgument();

        JzCampaignAssigned campaignAssigned = null;

        if (id == null || id == 0) {
            campaignAssigned = new JzCampaignAssigned();
        } else
            campaignAssigned = campaignAssignedService.findById(id);

        campaignAssigned.setCampaignId(campaignId);
        campaignAssigned.setUpdateTime(LocalDateTime.now());
        campaignAssigned.setUpdateUser(SecurityHelper.getAdminId());
        campaignAssigned.setTitle(title);
        campaignAssigned.setDeleted(false);
        Integer[] shops = null;
        if (assignedShops != null && assignedShops.size() > 0)
            shops = assignedShops.toArray(new Integer[0]);
        campaignAssigned.setShops(shops);


        Integer[] skills = null;
        if (assignedSkillGroup != null && assignedSkillGroup.size() > 0)
            skills = assignedSkillGroup.toArray(new Integer[0]);
        campaignAssigned.setSkills(skills);

        if(id==null||id==0) {
            campaignAssigned.setCreateUser(SecurityHelper.getAdminId());
            campaignAssigned.setCreateTime(LocalDateTime.now());
            campaignAssignedService.add(campaignAssigned);
        }
        else
            campaignAssignedService.update(campaignAssigned);


        //查询所有历史数据
      /*  List<JzCampaignAssigned> oldAssigned = campaignAssignedService.list(campaignId);
        for (JzCampaignAssigned assigned : oldAssigned) {
            assigned.setUpdateTime(LocalDateTime.now());
            assigned.setUpdateUser(SecurityHelper.getAdminId());
            assigned.setDeleted(true);
            campaignAssignedService.update(assigned);
        }

        //插入分配技能组的数据
        for (Integer id : assignedSkillGroup) {
            campaignAssignedService.add(inti(campaignId, id, 0));
        }
        //插入分配到坐席的数据
        for (Integer id : assignedUsers) {
            campaignAssignedService.add(inti(campaignId, 0, id));
        }*/

        return ResponseUtil.ok();
    }

    @GetMapping("/allShopsAndSkills")
    public Object getAllShopsAndSkills(Integer campaignId,Integer assignId) {
        //获取此任务下 已经分配的门店
       return adminCamapaignAssignedService.getAllShopsAndSkills(campaignId,assignId);
    }


}
