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.PhoneNumberUtil;
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.JzBuiltList;
import org.linlinjava.jz.db.domain.JzBuiltListCount;
import org.linlinjava.jz.db.domain.SysUser;
import org.linlinjava.jz.db.service.JzBuiltListService;
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.lang.ref.PhantomReference;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Api(value = "Admin-uiltList-Controller", description = "电访任务结果处理")
@RestController
@RequestMapping("/admin/built")
@Validated
public class AdminBuiltListController {

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

    @Autowired
    private JzBuiltListService builtListService;

    @RequiresPermissions("admin:built:list")
    @RequiresPermissionsDesc(menu = {"电访中心", "电访任务"}, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(required = false) String mobile,
                       @NotNull @RequestParam(required = false) Integer campaignId,
                       @RequestParam(required = false) List<Short> callResultArray,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        List<JzBuiltList> builtLists = builtListService.querySelective(SecurityHelper.getAdminId(), mobile, campaignId, callResultArray, page, limit, sort, order);
        for (JzBuiltList builtList : builtLists) {
            if (builtList.getCustomer() != null)
                builtList.getCustomer().setTel(PhoneNumberUtil.HideMobile(builtList.getCustomer().getTel()));
        }
        return ResponseUtil.okList(builtLists);
    }

    /**
     * 返回坐席任务名及数量 初始化任务tab
     *
     * @return
     */
    @GetMapping("/count")
    public Object count() {
        SysUser admin = (SysUser) SecurityUtils.getSubject().getPrincipal();
        List<JzBuiltListCount> campaigns = builtListService.countByCampaignName(admin.getId().intValue());
        return ResponseUtil.ok(campaigns);
    }

    @RequiresPermissions("admin:built:create")
    @RequiresPermissionsDesc(menu = {"电访中心", "电访任务"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody JzBuiltList builtList) {
        builtList.setCreateUser(SecurityHelper.getAdminId());
        builtList.setLastCallTime(LocalDateTime.now());
        builtListService.add(builtList);
        return ResponseUtil.ok();
    }

}
