package com.tofly.xrtygis.custom.controller.call;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.xrtygis.callCenterMapper.PltCalllist1000Mapper;
import com.tofly.xrtygis.config.CallCenterFeign;
import com.tofly.xrtygis.entity.Gdreturn;
import com.tofly.xrtygis.entity.Pfrecord;
import com.tofly.xrtygis.entity.callCenter.PltCalllist1000;
import com.tofly.xrtygis.service.GdreturnService;
import com.tofly.xrtygis.service.PfrecordService;
import com.tofly.xrtygis.service.callCenter.service.PltCalllist1000Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author FlashWJL
 * @date 2021/6/17 19:36
 */
@RestController
@AllArgsConstructor
@RequestMapping("/grade")
@Api(tags = "热线--质检、回访业务")
public class GradeController {

    private final PfrecordService pfrecordService;
    private final GdreturnService gdreturnService;
    private final PltCalllist1000Service pltCalllist1000Service;


    @ApiOperation(value = "填写工单回访")
    @PostMapping("/gdReturn")
    @ToFlyAppLog(title = "填写工单回访")
    public ResultRespone gdReturn(@RequestBody Gdreturn gdreturn) {

        return ResultRespone.success(gdreturnService.gdReturn(gdreturn));
    }

    @ApiOperation(value = "查看工单回访记录")
    @GetMapping("/gdReturn")
    @ToFlyAppLog(title = "查看工单回访记录")
    public ResultRespone getReturn(Page page, Gdreturn gdreturn) {
        return ResultRespone.success(gdreturnService.page(page, Wrappers.query(gdreturn)));
    }


    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param pfrecord 评分记录表
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "质检分页查询")
    @ToFlyAppLog(title = "质检分页查询")
    public ResultRespone getPfrecordPage(Page page, @ModelAttribute Pfrecord pfrecord) {
        return ResultRespone.success(pfrecordService.page(page, Wrappers.query(pfrecord)));
    }

    /**
     * 新增评分记录表
     *
     * @param pfrecord 评分记录表
     * @return ResultRespone
     */
    //(title = "新增评分记录表")
    @ApiOperation(value = "新增质检记录表")
    @PostMapping("/addGrade")
    @Transactional(rollbackFor = Exception.class)
    @ToFlyAppLog(title = "新增质检记录表")
    public ResultRespone addGrade(@RequestBody Pfrecord pfrecord) {
        String pfType = pfrecord.getPfType();
        String pfZxry = pfrecord.getPfZxry();
        String pfLyid = pfrecord.getPfLyid();
        String pfGdbh = pfrecord.getPfGdbh();
        if (StringUtil.isEmpty(pfType)) {
            throw new ToflyDeniedException("请传入评分类型");
        }
        //如果是工单评分，则判断工单编号
        if ("1".equals(pfType)) {
            if (StringUtil.isEmpty(pfGdbh)) {
                throw new ToflyDeniedException("请传入工单编号");
            }
            //判断该工单是否质检记录过，如果有记录，则不能再次质检
            Pfrecord gdPf = pfrecordService.getOne(Wrappers.<Pfrecord>lambdaQuery().eq(Pfrecord::getPfGdbh, pfGdbh));
            if (StringUtil.isNotNull(gdPf)) {
                throw new ToflyDeniedException("该工单已质检");
            }
        }
        //如果是录音评分，需要判断录音
        else if ("2".equals(pfType)) {
            if (StringUtil.isEmpty(pfZxry)) {
                throw new ToflyDeniedException("请传入评分坐席人员");
            }
            if (StringUtil.isEmpty(pfLyid)) {
                throw new ToflyDeniedException("请传入录音编码ID");
            }
            //判断该工单是否质检记录过，如果有记录，则不能再次质检
            Pfrecord lyPf = pfrecordService.getOne(Wrappers.<Pfrecord>lambdaQuery().eq(Pfrecord::getPfLyid, pfLyid));
            if (StringUtil.isNotNull(lyPf)) {
                throw new ToflyDeniedException("该录音已质检");
            }
//            Integer fwzc = pfrecord.getFwzc();
//            Integer zzkg = pfrecord.getZzkg();
//            Integer fwyy = pfrecord.getFwyy();
//            Integer yyyd = pfrecord.getYyyd();
//            Integer jdwz = pfrecord.getJdwz();
//            Integer jdzq = pfrecord.getJdzq();
//            Integer qtlj = pfrecord.getQtlj();
//            Integer xxsj = pfrecord.getXxsj();
//            Integer xxqr = pfrecord.getXxqr();
//            Integer dhjq = pfrecord.getDhjq();
//            Integer zlk  = pfrecord.getZlk();
//            Integer lcgf = pfrecord.getLcgf();
//            Integer zf = fwzc + zzkg + fwyy + yyyd + jdwz + jdzq + qtlj + xxsj + xxqr + dhjq + zlk + lcgf ;
//            pfrecord.setPfFz(zf);
        }
        //评分人员
        Long pfry = SecurityUtils.getUserId();
        pfrecord.setPfsj(Date8Util.getCurrentDate());
        pfrecord.setPfry(pfry);
        //保存质检记录
        pfrecordService.save(pfrecord);
        //修改录音标识
        boolean isGrade = false;
        if ("2".equals(pfType)) {
            //远程调用修改评分状态
            isGrade = pltCalllist1000Service.update(Wrappers.<PltCalllist1000>lambdaUpdate()
                    .eq(PltCalllist1000::getConnectionid, pfLyid)
                    .set(PltCalllist1000::getIsGrade, "1"));
            if (!isGrade) {
                //如果这里抛异常，则会回滚保存的录音质检记录
                throw new ToflyDeniedException("录音质检失败");
            }
        }
        //修改工单标识
        boolean isGd = false;
        if ("1".equals(pfType)) {
            isGd = pfrecordService.updateGdQualityState(pfGdbh);
            if (!isGd) {
                throw new ToflyDeniedException("工单质检失败");
            }
        }
        return ResultRespone.success(true);
    }


}
