package com.yida.data.device.telephone.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.annotation.ControllerLog;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.entity.telephone.EduTelephone;
import com.yida.data.common.core.entity.telephone.EduTelephoneBlack;
import com.yida.data.common.core.entity.telephone.EduTelephoneCard;
import com.yida.data.common.core.entity.telephone.EduTelephonePeriod;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.device.dto.telephone.DevicePage;
import com.yida.data.device.dto.telephone.PhoneStatusDTO;
import com.yida.data.device.dto.telephone.TelephoneDTO;
import com.yida.data.device.telephone.service.EduTelephoneBlackService;
import com.yida.data.device.telephone.service.EduTelephonePeriodService;
import com.yida.data.device.telephone.service.EduTelephoneService;
import com.yida.data.device.vo.OnlineCountVO;
import com.yida.data.device.vo.telephone.DeviceNumberVO;
import com.yida.data.device.vo.telephone.TelephoneVO;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.system.feign.RemoteDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 亲情电话-话机管理 Controller
 *
 * @author ccl
 * @date 2021-08-10 16:27:43
 */
@Api("亲情电话-话机")
@Slf4j
@Validated
@RestController
@RequestMapping("/telephone/telephone")
@RequiredArgsConstructor
public class EduTelephoneController {

    private final EduTelephoneService eduTelephoneService;
    private final EduTelephoneBlackService eduTelephoneBlackService;
    private final EduTelephonePeriodService eduTelephonePeriodService;

    private final RemoteDeptService remoteDeptService;

    @ApiOperation("话机列表")
    @GetMapping("/telephoneList")
    public ResultBean<DevicePage<TelephoneVO>> telephoneList(
        @ApiParam("话机状态,0-在线，1-离线") @RequestParam(required = false) String onlineStatus,
        @ApiParam("运营状态，0-正常，1-停运") @RequestParam(required = false) String runStatus,
        @ApiParam("iccIdAndiMei") @RequestParam(required = false) String iccIdAndiMei,
        @ApiParam("话机名称") @RequestParam(required = false) String name,
        @RequestParam(defaultValue = "10") Integer pageSize,
        @RequestParam(defaultValue = "1") Integer pageNum,
        @ApiParam("学校id") @RequestParam(required = false) Long schoolId,
        @ApiParam("学校名称") @RequestParam(required = false) String schoolName)
        throws NoSuchMethodException {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        return ResultBean.buildSuccess(
            eduTelephoneService.telephoneList(new DevicePage<>(pageNum, pageSize), onlineStatus, runStatus, iccIdAndiMei,
                name, currentUser, schoolId, schoolName));
    }

    /**
     * 话机设备数量统计
     *
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.attendance.vo.DeviceNumberVO>
     * @author ZYJ
     * @date 2021/10/9 15:17
     */
    @ApiOperation("话机设备数量统计")
    @GetMapping("/findDeviceNumber")
    public ResultBean<DeviceNumberVO> findDeviceNumber() throws NoSuchMethodException {
        return eduTelephoneService.findDeviceNumber();
    }

    @ApiOperation("修改或新增")
    @PostMapping("/insertTelephone")
    @OperationLog(module = ModuleName.TELEPHONE, methods = "修改或新增亲情电话", type = OperationLogTypeEnum.SAVE)
    public ResultBean insertTelephone(@RequestBody EduTelephone eduTelephone) {
        eduTelephoneService.saveOrUpdate(eduTelephone);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("批量设置")
    @PostMapping("/updateTelephone")
    @OperationLog(module = ModuleName.TELEPHONE, methods = "批量设置亲情电话", type = OperationLogTypeEnum.UPDATE)
    public ResultBean updateTelephone(@RequestBody TelephoneDTO telephoneDTO) {
        eduTelephoneService.updateTelephone(telephoneDTO);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("话机详情")
    @GetMapping("/getTelephone")
    public ResultBean<TelephoneVO> getTelephone(@ApiParam("话机Id") @RequestParam Long id) {

        return ResultBean.buildSuccess(eduTelephoneService.getByTelephoneId(id));
    }

    @ApiOperation("重置本月通话时间")
    @GetMapping("/delTelephoneTime")
    @OperationLog(module = ModuleName.TELEPHONE, methods = "重置本月通话时间", type = OperationLogTypeEnum.UPDATE)
    public ResultBean delTelephoneTime(@ApiParam("话机Id") @RequestParam Long id) {
        EduTelephone eduTelephone = new EduTelephone();
        eduTelephone.setId(id);
        eduTelephone.setTotalCallTime(0);
        eduTelephoneService.updateById(eduTelephone);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("停运")
    @GetMapping("/stopTelephone")
    @OperationLog(module = ModuleName.TELEPHONE, methods = "停运亲情电话", type = OperationLogTypeEnum.UPDATE)
    public ResultBean stopTelephone(@ApiParam("话机Id") @RequestParam Long id,
        @ApiParam("1停运，0正常") @RequestParam Integer status) {

        EduTelephone eduTelephone = new EduTelephone();
        eduTelephone.setId(id);
        eduTelephone.setRunStatus(status);
        return ResultBean.buildSuccess(eduTelephoneService.updateById(eduTelephone));
    }

    @ApiOperation("删除和批量删除")
    @PostMapping("/delTelephone")
    @OperationLog(module = ModuleName.TELEPHONE, methods = "删除和批量删除亲情电话", type = OperationLogTypeEnum.DELETE)
    public ResultBean delTelephone(@ApiParam("ids") @RequestBody List<Long> ids) {
        eduTelephoneService.removeByIds(ids);
        for (Long id : ids) {
            eduTelephonePeriodService
                .remove(Wrappers.lambdaQuery(new EduTelephonePeriod()).eq(EduTelephonePeriod::getTelephoneId, id));
            eduTelephoneBlackService
                .remove(Wrappers.lambdaQuery(new EduTelephoneBlack()).eq(EduTelephoneBlack::getTelephoneId, id));
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("批量停运")
    @PostMapping("/listStopTelephone")
    @OperationLog(module = ModuleName.TELEPHONE, methods = "批量停运亲情电话", type = OperationLogTypeEnum.UPDATE)
    public ResultBean listStopTelephone(@ApiParam("话机Id") @RequestBody PhoneStatusDTO phoneStatusDTO) {

        List<Long> ids = phoneStatusDTO.getIds();
        ArrayList<EduTelephone> list = new ArrayList<>();
        for (Long id : ids) {
            EduTelephone eduTelephone = new EduTelephone();
            eduTelephone.setId(id);
            eduTelephone.setRunStatus(phoneStatusDTO.getStatus());
            list.add(eduTelephone);
        }
        eduTelephoneService.updateBatchById(list);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("家长的学生列表")
    @GetMapping("/listStudentByParent")
    public ResultBean<List<EduTelephoneCard>> listStudentByParent(@ApiParam("家长id") @RequestParam Long parentId,
        @ApiParam("学校id") @RequestParam Long schoolId) {
        return ResultBean.buildSuccess(eduTelephoneService.getStudentList(parentId, schoolId));
    }

    @ApiOperation("获取话机在线分布")
    @GetMapping("/getOnlineDistrib")
    public ResultBean<OnlineCountVO> getOnlineDistrib(){
        List<Long> schoolIds = remoteDeptService.getSchoolByArea(null, null).getData().stream().map(Dept::getDeptId).collect(
            Collectors.toList());
        return ResultBean.buildSuccess(eduTelephoneService.getOnlineDistrib(schoolIds));
    }

}
