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

import com.alibaba.fastjson.JSON;
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.controller.BaseController;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.daily.PlanregionService;
import com.tofly.pojo.plan.Planregion;
import com.tofly.pojo.plan.dto.PlanregionDto;
import com.tofly.pojo.plan.vo.PlanregionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Optional;

/**
 * 巡检片区信息表：TF_YWPN_PLANREGION_W
 *
 * @author sunjian
 * @date Tue Mar 07 00:00:00 CST 2023
 */
@RestController
@RequestMapping("/planregion")
@Api(tags = "巡检片区信息")
public class PlanregionController extends BaseController<Planregion, PlanregionVo> {

    @Resource
    private PlanregionService planregionService;

    @Autowired
    public PlanregionController(PlanregionService planregionService) {
        init(planregionService);
    }

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param planregionDto 巡检片区信息表：TF_YWPN_PLANREGION_W
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone<Page<PlanregionVo>> getPlanregionPage(Page page, @ModelAttribute PlanregionDto planregionDto) {
        ResultRespone<Page<PlanregionVo>> resultRespone = new ResultRespone<>();
        try {
            Page<PlanregionVo> planPointPage = planregionService.getPlanPointPage(page, planregionDto);
            resultRespone.setResult(planPointPage);
        } catch (BusinessException businessException) {
            resultRespone.setCode(businessException.getRetCd());
            resultRespone.setMessage(businessException.getMsgDes());
        }
        return resultRespone;
    }


    /**
     * 通过id查询巡检片区信息表：TF_YWPN_PLANREGION_W
     *
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone<Planregion> getById(@PathVariable("id") Long id) {
        ResultRespone<Planregion> resultRespone = new ResultRespone<>();
        try {
            return super.getById(id);
        } catch (BusinessException businessException) {
            resultRespone.setMessage(businessException.getMsgDes());
            resultRespone.setCode(businessException.getRetCd());
        }
        return resultRespone;
    }

    /**
     * 新增巡检片区信息表：TF_YWPN_PLANREGION_W
     *
     * @param planregionDto 巡检片区信息表：TF_YWPN_PLANREGION_W
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增巡检片区信息表")
    @ApiOperation(value = "新增巡检片区信息表")
    @PostMapping
    public ResultRespone<Object> save(@Valid @RequestBody PlanregionDto planregionDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            try {
                return super.save(JSON.parseObject(JSON.toJSONString(planregionDto), Planregion.class));
            } catch (BusinessException e) {
                rest.setCode(e.getRetCd());
                rest.setMessage(e.getMsgDes());
            }
        }
        return rest;
    }

    /**
     * 修改巡检片区信息表：TF_YWPN_PLANREGION_W
     *
     * @param planregionDto 巡检片区信息表：TF_YWPN_PLANREGION_W
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改巡检片区信息表")
    @ApiOperation(value = "修改巡检片区信息表")
    @PutMapping
    public ResultRespone<Object> updateById(@Valid @RequestBody PlanregionDto planregionDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            try {
                return super.updateById(JSON.parseObject(JSON.toJSONString(planregionDto), Planregion.class));
            } catch (BusinessException e) {
                rest.setCode(e.getRetCd());
                rest.setMessage(e.getMsgDes());
            }
        }
        return rest;
    }

    /**
     * 通过id批量删除巡检片区信息表：TF_YWPN_PLANREGION_W
     *
     * @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<Object> removeById(String ids) {
        ResultRespone<Object> resultRespone = new ResultRespone<>();
//        try {
//            return planregionService.removeByIds(ids);
//        } catch (BusinessException businessException) {
//            resultRespone.setCode(businessException.getRetCd());
//            resultRespone.setMessage(businessException.getMsgDes());
//        }
        try {
            return super.removeByIdList(ids);
        } catch (BusinessException e) {
            resultRespone.setCode(e.getRetCd());
            resultRespone.setMessage(e.getMsgDes());
        }
        return resultRespone;
    }
}
