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.SecurityUtils;
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.JzShop;
import org.linlinjava.jz.db.service.JzShopService;
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-Shop-Controller",description = "门店信息维护")
@RestController
@RequestMapping("/admin/shop")
@Validated
public class AdminShopController {

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

    @Autowired
    private JzShopService shopService;


    @RequiresPermissions("admin:shop:list")
    @RequiresPermissionsDesc(menu = {"配置管理", "门店配置"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name,@RequestParam(defaultValue = "0") Integer shopCity,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        List<JzShop> shopList = shopService.querySelective(name,shopCity, page, limit, sort, order);
        return ResponseUtil.okList(shopList);
    }

    @RequiresPermissions("admin:shop:create")
    @RequiresPermissionsDesc(menu = {"配置管理", "门店配置"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody JzShop shop) {
        shop.setCreateUser(SecurityHelper.getAdminId());
        shop.setUpdateUser(SecurityHelper.getAdminId());
        shop.setDeleted(false);
        shopService.add(shop);
        return ResponseUtil.ok(shop);
    }

    @RequiresPermissions("admin:shop:update")
    @RequiresPermissionsDesc(menu = {"配置管理", "门店配置"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody JzShop shop) {
        shop.setUpdateUser(SecurityHelper.getAdminId());
        shopService.updateById(shop);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:shop:delete")
    @RequiresPermissionsDesc(menu = {"配置管理", "门店配置"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody JzShop shop) {
        shop.setUpdateUser(SecurityHelper.getAdminId());
        shop.setDeleted(true);
        shopService.updateById(shop);
        return ResponseUtil.ok();
    }

}
