package org.linlinjava.jz.admin.web;


import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.jz.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.jz.admin.util.SecurityHelper;
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.JzDmChannel;
import org.linlinjava.jz.db.service.JzDmChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "Admin-Dm-Channel-Controller",description = "业务渠道维护")
@RestController
@RequestMapping("/admin/channel")
@Validated
public class AdminDmChannelController {

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

    @Autowired
    private JzDmChannelService channelService;

    @RequiresPermissions("admin:source:list")
    @RequiresPermissionsDesc(menu = {"配置管理", "业务渠道"}, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        List<JzDmChannel> channelList = channelService.querySelective( page, limit, sort, order);
        return ResponseUtil.okList(channelList);
    }

    @RequiresPermissions("admin:source:create")
    @RequiresPermissionsDesc(menu = {"配置管理", "业务渠道"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody JzDmChannel source) {
        source.setCreateUser(SecurityHelper.getAdminId());
        source.setDeleted(false);
        channelService.add(source);
        return ResponseUtil.ok(source);
    }

    @RequiresPermissions("admin:channel:update")
    @RequiresPermissionsDesc(menu = {"配置管理", "业务渠道"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody JzDmChannel source) {
        channelService.update(source);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:channel:delete")
    @RequiresPermissionsDesc(menu = {"配置管理", "业务渠道"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody JzDmChannel source) {
        source.setDeleted(true);
        channelService.delete(source);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:channel:all")
    @RequiresPermissionsDesc(menu = {"配置管理", "业务渠道"}, button = "获取所有")
    @GetMapping("/all")
    public Object list(){
        return ResponseUtil.ok(channelService.all());
    }
}
