package com.mosty.qwzx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.base.model.dto.qwzx.TbQwCjbsSaveDTO;
import com.mosty.base.model.dto.qwzx.TbQwCjbsUpdateDTO;
import com.mosty.base.model.entity.qwzx.TbQwCjbs;
import com.mosty.base.model.query.qwzx.TbQwCjbsQuery;
import com.mosty.base.utils.spring.SpringValidUtils;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.entity.log.StringUtils;
import com.mosty.common.token.JwtSysUser;
import com.mosty.qwzx.service.TbQwCjbsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author zhangzhao
 * 勤务管理-处警报送接口
 */
@Api(tags = "处警报送")
@RestController
@AllArgsConstructor
@RequestMapping("/tbQwCjbs")
public class TbQwCjbsController {

    /**
     * 勤务管理处警报送表Service
     */
    private final TbQwCjbsService tbQwCjbsService;

    /**
     * 查询单条（根据主键ID）
     * @param id 主键ID
     * @return ResponseResult 处警报送信息
     */
    @ApiOperation(value = "查询单条（根据主键ID）")
    @GetMapping("{id}")
    @JwtSysUser
    @Log(title = "勤务管理-处警报送接口-查询单条（根据主键ID）", businessType = BusinessType.OTHER)
    public ResponseResult<TbQwCjbs> selectById(@PathVariable("id") String id){
        return ResponseResult.success(tbQwCjbsService.selectById(id));
    }

    /**
     * 查询今日数量
     * @param ssbmdm 所属部门代码
     * @param isChild 是否包含下级
     * @return ResponseResult 今日数量
     */
    @ApiOperation(value = "查询今日数量")
    @GetMapping("/selectCountToday")
    @JwtSysUser
    @Log(title = "勤务管理-处警报送接口-查询今日数量", businessType = BusinessType.OTHER)
    public ResponseResult<Integer> selectCountToday(@PathParam("ssbmdm") String ssbmdm, @PathParam("isChild") String isChild){
        return ResponseResult.success(tbQwCjbsService.selectCountToday(ssbmdm, isChild));
    }

    /**
     * 查询分页
     * @param query 处警报送查询对象
     * @return ResponseResult 处警报送信息分页列表
     */
    @ApiOperation(value = "查询分页")
    @GetMapping("/selectPage")
    @JwtSysUser
    @Log(title = "勤务管理-处警报送接口-查询分页", businessType = BusinessType.OTHER)
    public ResponseResult<IPage<TbQwCjbs>> selectPage(TbQwCjbsQuery query){
        return ResponseResult.success(tbQwCjbsService.selectPage(query));
    }

    /**
     * 查询列表
     * @param query 处警报送查询对象
     * @return ResponseResult 处警报送信息列表
     */
    @ApiOperation(value = "查询列表")
    @GetMapping("/selectList")
    @JwtSysUser
    @Log(title = "勤务管理-处警报送接口-查询列表", businessType = BusinessType.OTHER)
    public ResponseResult<List<TbQwCjbs>> selectList(TbQwCjbsQuery query){
        return ResponseResult.success(tbQwCjbsService.selectList(query));
    }

    /**
     * 保存单条
     * @param dto 处警报送保存对象
     * @param bindResult 校验对象
     * @return ResponseResult 处警报送主键ID
     */
    @ApiOperation(value = "保存单条")
    @PostMapping("save")
    @JwtSysUser
    @Log(title = "勤务管理-处警报送接口-保存单条", businessType = BusinessType.INSERT)
    public ResponseResult<String> save(@RequestBody @Valid TbQwCjbsSaveDTO dto, BindingResult bindResult){
        try {
            String message = SpringValidUtils.getErrorsMsg(bindResult);
            if(StringUtils.isNotBlank(message)){
                return ResponseResult.fail("保存失败！" + message);
            }

            //保存
            String resultId = tbQwCjbsService.saveEntity(dto);
            if(StringUtils.isNotBlank(resultId)){
                return ResponseResult.success(resultId);
            }
            return  ResponseResult.fail("保存失败！");
        } catch (Exception e) {
            return ResponseResult.fail("保存失败！服务异常");
        }
    }

    /**
     * 修改单条
     * @param dto 处警报送修改对象
     * @param bindResult 校验对象
     * @return ResponseResult 处警报送主键ID
     */
    @ApiOperation(value = "修改单条")
    @PostMapping("update")
    @JwtSysUser
    @Log(title = "勤务管理-处警报送接口-修改单条", businessType = BusinessType.UPDATE)
    public ResponseResult<String> update(@RequestBody @Valid TbQwCjbsUpdateDTO dto, BindingResult bindResult){
        try {
            String message = SpringValidUtils.getErrorsMsg(bindResult);
            if(StringUtils.isNotBlank(message)){
                return ResponseResult.fail("修改失败！" + message);
            }

            //修改
            String resultId = tbQwCjbsService.updateEntity(dto);
            if(StringUtils.isNotBlank(resultId)){
                return ResponseResult.success(resultId);
            }
            return  ResponseResult.fail("修改失败！");
        } catch (Exception e) {
            return ResponseResult.fail("修改失败！服务异常");
        }
    }

    /**
     * 注销单条
     * @param id 主键ID
     * @return ResponseResult 注销条数
     */
    @ApiOperation(value = "注销单条")
    @DeleteMapping("{id}")
    @JwtSysUser
    @Log(title = "勤务管理-处警报送接口-注销单条", businessType = BusinessType.DELETE)
    public ResponseResult<Integer> update(@PathVariable("id") String id){
        try {
            //如果传入的ID为空，返回失败
            if(StringUtils.isBlank(id)){
                return ResponseResult.fail("注销失败！" + "【主键ID】不能为空");
            }

            //如果已经注销，则返回成功
            TbQwCjbs entity = tbQwCjbsService.selectById(id);
            if(entity == null){
                return ResponseResult.success(0);
            }

            //注销
            return ResponseResult.success(tbQwCjbsService.cancelEntity(entity));
        } catch (Exception e) {
            return ResponseResult.fail("注销失败！服务异常");
        }
    }

}
