package org.linlinjava.jz.admin.web;

import io.swagger.annotations.Api;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.jz.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.jz.admin.dto.CustomerDTO;
import org.linlinjava.jz.admin.service.AdminCustomerService;
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.CustomerClient;
import org.linlinjava.jz.db.domain.CustomerFn;
import org.linlinjava.jz.db.service.CustomerClientService;
import org.linlinjava.jz.db.service.JzCustomerFnService;
import org.linlinjava.jz.db.util.RandUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(value = "Admin-Customer-Controller", description = "客户管理中心")
@RestController
@RequestMapping("/admin/customer")
@Validated
public class AdminCustomerController {
    private final Log logger = LogFactory.getLog(AdminCustomerController.class);

    @Autowired
    private AdminCustomerService userService;


    @RequiresPermissions("admin:customer:list")
    @RequiresPermissionsDesc(menu = {"用户中心", "客户管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String username, String mobile,
                       Integer type,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return userService.list(username, mobile, type, page, limit, sort, order);
    }

    /**
     * 创建客户
     *
     * @param customerDTO
     * @return
     */
    @RequiresPermissions("admin:customer:create")
    @RequiresPermissionsDesc(menu = {"用户中心", "客户管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody CustomerDTO customerDTO) {
        return userService.create(customerDTO.getUser(), customerDTO.getFns());
    }

    @RequiresPermissions("admin:customer:update")
    @RequiresPermissionsDesc(menu = {"用户中心", "客户管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody CustomerDTO customerDTO) {
        userService.update(customerDTO.getUser(), customerDTO.getFns());
        return ResponseUtil.ok();
    }

    /**
     * 根据进电号码查询客户id 如果没有查询到则新增一条客户信息 然后返回新增的id
     *
     * @param mobile
     * @return
     */
    @GetMapping("/find")
    public Object findByMobile(@NotNull @RequestParam String mobile) {
        return ResponseUtil.ok(userService.findByMobile(mobile));
    }

    /**
     * 返回客户详情
     *
     * @param id
     * @return
     */
    @GetMapping("/read")
    public Object read(@NotNull @RequestParam Integer id) {
        return ResponseUtil.ok(userService.read(id));
    }

    /**
     * 返回客户详情
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:customer:detail")
    @RequiresPermissionsDesc(menu = {"用户中心", "客户管理"}, button = "详情")
    @GetMapping("/detail")
    public Object detail(@NotNull @RequestParam Integer id) {
        return ResponseUtil.ok(userService.detail(id));
    }

    /**
     * 进电时使用，如果不存在此手机号的客户，新增一个客户并返回id
     *
     * @param mobile
     * @return
     */
    @GetMapping("/findOrAdd")
    public Object findOrAdd(@NotNull @RequestParam String mobile) {
        return userService.findOrAdd(mobile);
    }

}
