package org.linlinjava.jz.admin.web;

import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.jz.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.jz.admin.service.AdminShopService;
import org.linlinjava.jz.admin.service.AdminSkillGroupService;
import org.linlinjava.jz.admin.util.SecurityHelper;
import org.linlinjava.jz.admin.vo.ShopVo;
import org.linlinjava.jz.admin.vo.SkillGroupVo;
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.linlinjava.jz.db.service.JzCampaignService;
import org.linlinjava.jz.db.service.JzSkillGroupService;
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-Campaign-Controller",description = "任务维护")
@RestController
@RequestMapping("/admin/campaign")
@Validated
public class AdminCampaignController {

    private final Log logger = LogFactory.getLog(AdminCampaignController.class);

    @Autowired
    private JzCampaignService campaignService;

    @Autowired
    private JzSkillGroupService skillGroupService;



    @RequiresPermissions("admin:campaign:list")
    @RequiresPermissionsDesc(menu = {"会员营销", "任务管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        List<JzCampaign> campaignList = campaignService.querySelective(name, page, limit, sort, order);
        return ResponseUtil.okList(campaignList);
    }

    @RequiresPermissions("admin:campaign:create")
    @RequiresPermissionsDesc(menu = {"会员营销", "任务管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody JzCampaign campaign) {
        campaign.setCreateUser(SecurityHelper.getAdminId());
        campaign.setUpdateUser(SecurityHelper.getAdminId());
        campaign.setDeleted(false);
        campaignService.add(campaign);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:campaign:update")
    @RequiresPermissionsDesc(menu = {"会员营销", "任务管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody JzCampaign campaign) {
        campaign.setUpdateUser(SecurityHelper.getAdminId());
        campaignService.update(campaign);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:campaign:delete")
    @RequiresPermissionsDesc(menu = {"会员营销", "任务管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody JzCampaign campaign) {
        campaign.setUpdateUser(SecurityHelper.getAdminId());
        boolean deleted=!campaign.getDeleted();
        campaign.setDeleted(deleted);
        campaignService.update(campaign);
        return ResponseUtil.ok();
    }

    /**
     * 获取所有技能组 任务分配时使用
     * @return
     */
    @GetMapping("/allSkillGroup")
    public Object getSkillGroup( ) {
        return ResponseUtil.ok(skillGroupService.queryAll());
    }




}
