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.dto.CalllogAllinone;
import org.linlinjava.jz.admin.service.AdminCalllogService;
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.JzCalllog;
import org.linlinjava.jz.db.domain.JzCalllogRecord;
import org.linlinjava.jz.db.service.JzCampaignService;
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-Calllog-Controller",description = "电访记录、进电、外拨处理")
@RestController
@RequestMapping("/admin/calllog")
@Validated
public class AdminCalllogController {
    private final Log logger = LogFactory.getLog(AdminCalllogController.class);

    @Autowired
    private AdminCalllogService calllogService;

    @Autowired
    private JzCampaignService campaignService;


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

        List<JzCalllog> calllogList = calllogService.querySelective(mobile, callType, callResultArray,campaignId, page, limit, sort, order);

        //手机号码前三后四隐藏
        for (JzCalllog log : calllogList) {
            log.setMobile(PhoneNumberUtil.HideMobile(log.getMobile()));
        }
        return ResponseUtil.okList(calllogList);
    }

    @RequiresPermissions("admin:calllog:create")
    @RequiresPermissionsDesc(menu = {"电访中心", "电访记录"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody CalllogAllinone allinone) {
       return calllogService.create(allinone);
    }

    @RequiresPermissions("admin:calllog:createRecord")
    @RequiresPermissionsDesc(menu = {"电访中心", "话务记录添加"}, button = "添加")
    @PostMapping("/createRecord")
    public Object createRecord(@RequestBody JzCalllogRecord record) {
        return calllogService.add_record(record);
    }

    @RequiresPermissions("admin:calllog:updateRecord")
    @RequiresPermissionsDesc(menu = {"电访中心", "电访记录"}, button = "编辑")
    @PostMapping("/updateRecord")
    public Object update(@RequestBody JzCalllogRecord record) {
        return calllogService.updte_Record(record);
    }


    @RequiresPermissions("admin:calllog:campaigns")
    @RequiresPermissionsDesc(menu = {"电访中心", "电访记录"}, button = "任务筛选")
    @GetMapping("/campaigns")
    public Object campaigns(){
        return ResponseUtil.ok(campaignService.queryAll() );
    }

}
