package com.gxwebsoft.tower.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.entity.TowerPlace;
import com.gxwebsoft.tower.entity.TowerPlaceStandard;
import com.gxwebsoft.tower.param.TowerPlaceParam;
import com.gxwebsoft.tower.service.TowerPlaceService;
import com.gxwebsoft.tower.service.TowerPlaceStandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 检查/保养部位记录表控制器
 *
 * @author 科技小王子
 * @since 2023-06-08 11:00:17
 */
@Api(tags = "检查/保养部位记录表管理")
@RestController
@RequestMapping("/api/tower/tower-place")
public class TowerPlaceController extends BaseController {
    @Resource
    private TowerPlaceService towerPlaceService;
    @Resource
    private TowerPlaceStandardService towerPlaceStandardService;

    @PreAuthorize("hasAuthority('tower:towerPlace:list')")
    @OperationLog
    @ApiOperation("分页查询检查/保养部位记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerPlace>> page(TowerPlaceParam param) {
        PageParam<TowerPlace, TowerPlaceParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerPlaceService.page(page, page.getWrapper()));
        // 使用关联查询
//        return success(towerPlaceService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerPlace:list')")
    @OperationLog
    @ApiOperation("查询全部检查/保养部位记录表")
    @GetMapping()
    public ApiResult<List<TowerPlace>> list(TowerPlaceParam param) {
        PageParam<TowerPlace, TowerPlaceParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerPlaceService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerPlaceService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerPlace:list')")
    @OperationLog
    @ApiOperation("根据id查询检查/保养部位记录表")
    @GetMapping("/{id}")
    public ApiResult<TowerPlace> get(@PathVariable("id") Integer id) {
        return success(towerPlaceService.getById(id));
        // 使用关联查询
        //return success(towerPlaceService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerPlace:save')")
    @OperationLog
    @ApiOperation("添加检查/保养部位记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerPlace towerPlace) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          towerPlace.setUserId(loginUser.getUserId());
        }
        if (towerPlaceService.save(towerPlace)) {
            // 保存检查标准
            if (!towerPlace.getStandard().isEmpty()) {
              final List<TowerPlaceStandard> standards = towerPlace.getStandard();
              standards.forEach(d->{
                d.setPlaceId(towerPlace.getPlaceId());
              });
              towerPlaceStandardService.saveBatch(standards);
            }
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlace:update')")
    @OperationLog
    @ApiOperation("修改检查/保养部位记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerPlace towerPlace) {
        if (towerPlaceService.updateById(towerPlace)) {
            // 更新检查标准
            if (!towerPlace.getStandard().isEmpty()) {
              final List<TowerPlaceStandard> standards = towerPlace.getStandard();
              towerPlaceStandardService.saveOrUpdateBatch(standards);
            }
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlace:remove')")
    @OperationLog
    @ApiOperation("删除检查/保养部位记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerPlaceService.removeById(id)) {
            towerPlaceStandardService.remove(new LambdaQueryWrapper<TowerPlaceStandard>().eq(TowerPlaceStandard::getPlaceId,id));
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlace:save')")
    @OperationLog
    @ApiOperation("批量添加检查/保养部位记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerPlace> list) {
        if (towerPlaceService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlace:update')")
    @OperationLog
    @ApiOperation("批量修改检查/保养部位记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerPlace> batchParam) {
        if (batchParam.update(towerPlaceService, "place_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlace:remove')")
    @OperationLog
    @ApiOperation("批量删除检查/保养部位记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerPlaceService.removeByIds(ids)) {
            for (Integer id: ids){
              towerPlaceStandardService.remove(new LambdaQueryWrapper<TowerPlaceStandard>().eq(TowerPlaceStandard::getPlaceId,id));
            }
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
