package cn.iocoder.yudao.module.base.controller.admin;

import cn.iocoder.yudao.common.MessageSend;
import cn.iocoder.yudao.common.dto.MessageDTO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.app.controller.admin.NotifyMessageSocket;
import cn.iocoder.yudao.module.base.entity.RepairWorker;
import cn.iocoder.yudao.module.base.service.RepairWorkerService;
import cn.iocoder.yudao.module.base.vo.RepairWorkerPageReqVO;
import cn.iocoder.yudao.module.base.vo.RepairWorkerRespVO;
import cn.iocoder.yudao.module.base.vo.RepairWorkerSaveReqVO;
import cn.iocoder.yudao.module.system.api.user.dto.UserDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.common.BaseConstants.QUALS_INTERIM_PERIOD;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.ok;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 维修工人")
@RestController
@RequestMapping("/repair/worker")
@Validated
public class RepairWorkerController {

    @Resource
    private RepairWorkerService workerService;
    @Autowired
    private NotifyMessageSocket notifyMessageSocket;
    @Autowired
    private MessageSend messageSend;


    /**
     * 批量创建维修工人
     * @author PQZ
     * @date 18:32 2024/10/9
     * @param userList 选中的用户集合
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.String>
    **/
    @PostMapping("/create")
    @Operation(summary = "创建维修工人")
    public CommonResult<Boolean> createWorker(@RequestBody List<UserDTO> userList) {
        workerService.saveWorkers(userList);
        return success(true);
    }


    @PutMapping("/update")
    @Operation(summary = "更新维修工人")
    public CommonResult<Boolean> updateWorker(@RequestBody RepairWorkerSaveReqVO updateReqVO) {
        workerService.updateWorker(updateReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除维修工人")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteWorker(@RequestParam("id") String id) {
        workerService.deleteWorker(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得维修工人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<RepairWorkerRespVO> getWorker(@RequestParam("id") String id) {
        RepairWorker worker = workerService.getWorker(id);
        return success(BeanUtils.toBean(worker, RepairWorkerRespVO.class));
    }

    /**
     * 分页查询维修工人
     * @author PQZ
     * @date 14:10 2024/10/9
     * @param pageReqVO RepairWorkerPageReqVO实体
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.framework.common.pojo.PageResult<cn.iocoder.yudao.module.base.vo.RepairWorkerRespVO>>
    **/
    @GetMapping("/page")
    @Operation(summary = "获得维修工人分页")
    public CommonResult<IPage<?>> getWorkerPage(RepairWorkerPageReqVO pageReqVO,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<RepairWorkerRespVO> page = new Page<>(pageNo,pageSize);
        return success(workerService.queryListPage(pageReqVO,page));
    }

    /**
     * 通过工单ID查到工单ID中所选择的员工信息
     *
     * @author 小李
     * @date 15:53 2024/10/14
     * @param ticketId 工单ID
    **/
    @GetMapping("/listByTicketId")
    @Operation(summary = "通过工单ID查到工单ID中所选择的员工信息")
    public CommonResult<?> listByTicketId(@RequestParam("ticketId") String ticketId) {
        return success(workerService.listByTicketId(ticketId));
    }

    /**
     * 通过班组长的id查该班组的员工
     *
     * @author 小李
     * @date 16:55 2024/10/14
    **/
    @GetMapping("/listByLeadsId")
    @Operation(summary = "通过班组长的id查该班组的员工")
    public CommonResult<?> listByLeads() {
        return success(workerService.listByLeads());
    }

    /**
     * 查当前登录用户是否维修班组长
     * @author vinjor-M
     * @date 18:08 2024/10/16
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<?>
    **/
    @GetMapping("/getIfLeader")
    @Operation(summary = "查当前登录用户是否维修班组长")
    public CommonResult<?> getIfLeader() {
        return success(workerService.getIfLeader());
    }


    /**
     * 测试给维修工发送消息---测试
     * @author vinjor-M
     * @date 18:08 2024/10/16
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<?>
     **/
    @GetMapping("/sendMessage")
    @Operation(summary = "测试给维修工发送消息")
    public CommonResult<?> sendMessage(Long userId) {
        try {
            MessageDTO messageDTO = new MessageDTO();
            List<Long> userIds = new ArrayList<>();
            userIds.add(userId);
            messageDTO.setUserIds(userIds);
            messageDTO.setTemplateCode(QUALS_INTERIM_PERIOD);
            List<Object> paramList = new ArrayList<>();
            paramList.add("测试");
            paramList.add("3");
            messageDTO.setParamList(paramList);
            messageSend.send(messageDTO);
            notifyMessageSocket.sendMessage("11","180",userId.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ok();
    }
}