package com.fs.company.controller.company;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.Company;
import com.fs.company.domain.CompanyVoiceCaller;
import com.fs.company.param.CompanyVoiceCallerParam;
import com.fs.company.service.ICompanyService;
import com.fs.company.service.ICompanyVoiceCallerService;
import com.fs.company.vo.CompanyVoiceCallerListVO;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 企业用户坐席Controller
 *
 * @author fs
 * @date 2021-10-11
 */
@RestController
@RequestMapping("/company/companyVoiceCaller")
public class CompanyVoiceCallerController extends BaseController
{
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ICompanyVoiceCallerService companyVoiceCallerService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ICompanyService companyService;
    /**
     * 查询企业用户坐席列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyVoiceCallerParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyVoiceCallerListVO> list = companyVoiceCallerService.selectCompanyVoiceCallerListVO(param);
        return getDataTable(list);
    }

    /**
     * 导出企业用户坐席列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:export')")
    @Log(title = "企业用户坐席", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyVoiceCallerParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyVoiceCallerListVO> list = companyVoiceCallerService.selectCompanyVoiceCallerListVO(param);
        ExcelUtil<CompanyVoiceCallerListVO> util = new ExcelUtil<CompanyVoiceCallerListVO>(CompanyVoiceCallerListVO.class);
        return util.exportExcel(list, "companyCalling");
    }

    /**
     * 获取企业用户坐席详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:query')")
    @GetMapping(value = "/{callerId}")
    public AjaxResult getInfo(@PathVariable("callerId") Long callerId)
    {
        return AjaxResult.success(companyVoiceCallerService.selectCompanyVoiceCallerById(callerId));
    }

    /**
     * 新增企业用户坐席
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:bind')")
    @Log(title = "企业用户坐席", businessType = BusinessType.INSERT)
    @PostMapping("/bindCallerNo")
    public R bindCallerNo(@RequestBody CompanyVoiceCaller caller)
    {
//        String checkCode=redisTemplate.opsForValue().get(caller.getMobile())==null?"":redisTemplate.opsForValue().get(caller.getMobile()).toString();
//        if(!checkCode.equals(caller.getCode().toString())){
//            return R.error("验证码不正确");
//        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Company company=companyService.selectCompanyById(loginUser.getCompany().getCompanyId());
        //获取已分配置所有坐席数
        Integer counts=companyVoiceCallerService.selectCompanyVoiceCallerCountsByCompanyId(company.getCompanyId());
        if(counts>=company.getVoiceCallerNumber()){
            return R.error("没有可分配置的坐席号");
        }
        CompanyVoiceCaller checkCaller=companyVoiceCallerService.selectCompanyVoiceCallerById(caller.getCallerId());
        if(checkCaller.getStatus()==0){
            return R.error("此坐席号已绑定");
        }
        //处理 是否已添加
        CompanyVoiceCaller check=companyVoiceCallerService.selectCompanyVoiceCallerByUserId(loginUser.getCompany().getCompanyId(),caller.getCompanyUserId());
        if(check!=null){
            return R.error("此员工已绑定为坐席");
        }
        caller.setCallerNo(caller.getCallerNo());
        caller.setCompanyId(loginUser.getCompany().getCompanyId());
        caller.setCompanyUserId(caller.getCompanyUserId());
        caller.setStatus(0);
        caller.setBindTime(new Date());
        if(companyVoiceCallerService.updateCompanyVoiceCaller(caller)>0){
//            redisTemplate.opsForValue().set(caller.getMobile(),null);
            return R.ok("操作成功");
        }
        else
        {
            return R.error("操作失败");
        }

    }



    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:remove')")
    @Log(title = "企业用户坐席", businessType = BusinessType.DELETE)
	@DeleteMapping("/{callerIds}")
    public R remove(@PathVariable Long[] callerIds)
    {
        for(Long id:callerIds){
            CompanyVoiceCaller maps=new CompanyVoiceCaller();
            maps.setCallerId(id);
            maps.setCompanyId(0l);
            maps.setCompanyUserId(0l);
            maps.setMobile("");
            maps.setStatus(1);
            maps.setBindTime(null);
            companyVoiceCallerService.updateCompanyVoiceCaller(maps);
        }
        return R.ok("删除成功");
    }


    @GetMapping("/getCallerList")
    public R getCallerList()
    {
        List<CompanyVoiceCaller> list= companyVoiceCallerService.selectCompanyVoiceCallerByEnable();
        return R.ok().put("data",list);
    }







}
