/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.gpsboot.controller.maintain;

import com.baomidou.mybatisplus.core.toolkit.Assert;
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.log.annotation.ToFlyAppLog;
import com.tofly.gpsboot.common.system.DispatchEnum;
import com.tofly.gpsboot.common.system.DispatchStatusEnum;
import com.tofly.gpsboot.service.maintain.DispatchService;
import com.tofly.gpsboot.service.maintain.EventMangeService;
import com.tofly.gpsboot.service.maintain.FloodSeasonWService;
import com.tofly.gpsboot.service.mq.IPlanMessageService;
import com.tofly.pojo.maintain.Dispatch;
import com.tofly.pojo.maintain.EventMange;
import com.tofly.pojo.maintain.FloodSeasonW;
import com.tofly.pojo.maintain.dto.DispatchResolveDto;
import com.tofly.pojo.maintain.vo.DispatchVo;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 
 *
 * @author wangzhun
 * @date Wed May 11 00:00:00 CST 2022
 */
//@RestController
//@AllArgsConstructor
//@RequestMapping("/dispatch")
//@Api(tags="事件汛期派工")
public class DispatchController {
    private  IPlanMessageService iPlanMessageService;
    private  DispatchService dispatchService;

    private  FloodSeasonWService floodSeasonWService;

    private  EventMangeService eventMangeService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param dispatch 
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone<Page<DispatchVo>> getDispatchPage(Page page, @ModelAttribute Dispatch dispatch) {

        Page page1 = dispatchService.page(page, Wrappers.query(dispatch).<Dispatch>lambda()
                .orderByDesc(Dispatch::getCreateTime));
        List records = page1.getRecords();
        List<DispatchVo> list = dispatchService.viewConversion(records);
        page1.setRecords(list);
        return ResultRespone.success(page1);
    }


    /**
     * 通过id查询
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Integer")
    })
    public ResultRespone getById(@PathVariable("id" ) Integer id) {
      return ResultRespone.success();
    }
    @GetMapping("/getBySourceIdAndType" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "资源id，如事件上报id 或者汛情id", value = "sourceId", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "类型 1事件管理 2汛情管理", value = "sourceId", required = true, dataType = "String")
    })
    public ResultRespone getBySourceIdAndType(@Param("sourceId")Long sourceId,String type) {
        List<Dispatch> list = dispatchService.
                list(Wrappers.<Dispatch>lambdaQuery().eq(Dispatch::getType, type)
                        .eq(Dispatch::getSourceId, sourceId));
        List<DispatchVo> dispatchVos = dispatchService.viewConversion(list);
        return ResultRespone.success(dispatchVos);
    }
    /**
     * 新增
     * @param dispatch 
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增" )
    @ApiOperation(value = "新增")
    @PostMapping
    public ResultRespone save(@RequestBody Dispatch dispatch) {
           if(dispatchService.checkHasDispatched(dispatch)) {
               return  ResultRespone.failed("该数据已经派工");
           }

            dispatchService.save(dispatch);
            //更改事件 或者汛情的状态
            if(DispatchEnum.FLOOD_SEASON.getCode().equals(dispatch.getType())){
                FloodSeasonW byId = floodSeasonWService.getById(dispatch.getSourceId());
                byId.setStatus(DispatchStatusEnum.WAITRESOLVE.getCode());
                floodSeasonWService.updateById(byId);
            }
            if(DispatchEnum.EVENT_MANAGE.getCode().equals(dispatch.getType())){
                EventMange byId = eventMangeService.getById(dispatch.getSourceId());
                byId.setStatus(DispatchStatusEnum.WAITRESOLVE.getCode());
                eventMangeService.updateById(byId);
            }


         iPlanMessageService.eventAnFloodSendMessage(dispatch,"汛情事件提醒");

        return ResultRespone.success(dispatch);
    }

    /**
     * 修改
     * @param dispatch 
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改" )
    @ApiOperation(value = "修改")
    @PutMapping
    public ResultRespone updateById(@RequestBody Dispatch dispatch) {


      return ResultRespone.success(dispatchService.updateById(dispatch));
    }

    @ToFlyAppLog(title="派工工单处理" )
    @ApiOperation(value = "派工工单处理")
    @PutMapping("/updateStatusById")
    public ResultRespone dispatchResolve(@Valid DispatchResolveDto dispatchResolveDto,
                                          BindingResult result,
                                          @RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {

        Dispatch dispatchById = dispatchService.getById(dispatchResolveDto.getId());
        Assert.isTrue(dispatchById.getResolveDate()==null,"该派工已经处理");
        String sourceStatus = "";
        //2正在处理
        if("2".equals(dispatchResolveDto.getStatus())){
            sourceStatus = DispatchStatusEnum.RESOLVING.getCode();
        }else if("3".equals(dispatchResolveDto.getStatus())){
            sourceStatus = DispatchStatusEnum.OVER.getCode();
        }


        //更改事件 或者汛情的状态
        if(DispatchEnum.FLOOD_SEASON.getCode().equals(dispatchById.getType())){
            FloodSeasonW byId = floodSeasonWService.getById(dispatchById.getSourceId());
            byId.setStatus(sourceStatus);
            floodSeasonWService.updateById(byId);
        }
        if(DispatchEnum.EVENT_MANAGE.getCode().equals(dispatchById.getType())){
            EventMange byId = eventMangeService.getById(dispatchById.getSourceId());
            byId.setStatus(sourceStatus);
            eventMangeService.updateById(byId);
        }
        Long id = dispatchResolveDto.getId();


        dispatchService.uploadFile(dispatchById,fileList);
        dispatchById.setResolveDate(new Date());
        dispatchById.setResolveDetail(dispatchResolveDto.getResolveDetail());
        dispatchById.setStatus(dispatchResolveDto.getStatus());
        return ResultRespone.success(dispatchService.updateById(dispatchById));
    }

    /**
     * 通过id删除
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除" )
    @ApiOperation(value = "通过ID删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Integer")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Integer id) {
      return ResultRespone.success(dispatchService.removeById(id));
    }

    /**
     * 通过id批量删除
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id批量删除" )
    @ApiOperation(value = "通过ID批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(dispatchService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
