package com.yuchen.saas.park.service.controller.building;


import com.yuchen.saas.park.api.dto.building.PmgBatBuildingDynamicChangeStatusDto;
import com.yuchen.saas.park.api.dto.building.PmgBatBuildingDynamicQueryDto;
import com.yuchen.saas.park.api.entity.building.PmgBatBuildingDynamic;
import com.yuchen.saas.park.api.vo.building.PmgBatBuildingDynamicDetailVO;
import com.yuchen.saas.park.api.vo.building.PmgBatBuildingDynamicPageVO;
import com.yuchen.saas.park.service.service.building.IPmgBatBuildingDynamicService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.secure.ContextUser;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

/**
 *
 * 楼盘动态 相关接口
 *
 *
 * @author yuanzhelin
 * @since 2024-10-24
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/pmgBatBuildingDynamic")
public class PmgBatBuildingDynamicController {

    @Resource
    private IPmgBatBuildingDynamicService pmgBatBuildingDynamicService;

    /**
     * 新增/修改
     * @tag pauly-lyz
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增/修改")
    public R save(@Valid @RequestBody PmgBatBuildingDynamic dto) {
        ContextUser contextUser = AuthUtil.getUser();
        dto.setCreateUser(contextUser.getUserId());
        if (Func.isEmpty(dto.getId())){
            dto.setReleaseStatus(1);
            dto.setViewCount(0);
            dto.setShareNum(0);
        }
        return R.data(pmgBatBuildingDynamicService.saveOrUpdate(dto));
    }

    /**
     * 分页查询
     * @tag pauly-lyz
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询")
    public R<NestPage<PmgBatBuildingDynamicPageVO>> page(@RequestBody PmgBatBuildingDynamicQueryDto dto) {
        return R.data(NestPage.of(pmgBatBuildingDynamicService.page(dto)));
    }

    /**
     * 楼盘动态详情
     * @tag pauly-lyz
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "楼盘动态详情")
    public R<PmgBatBuildingDynamicDetailVO> detail(@PathVariable Long id) {
        PmgBatBuildingDynamic byId = pmgBatBuildingDynamicService.getByIdForUpdate(id);
        byId.setViewCount(byId.getViewCount()+1);
        pmgBatBuildingDynamicService.updateById(byId);
        return R.data(pmgBatBuildingDynamicService.detail(id));
    }

    /**
     * 删除
     * @tag pauly-lyz
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    @Transactional
    public R delete(@PathVariable Long id) {
        return R.data(pmgBatBuildingDynamicService.removeById(id));
    }

    /**
     * 修改状态
     * @tag pauly-lyz
     */
    @PutMapping("/change/status")
    @ApiOperation(value = "修改状态")
    public R changeStatus(@Valid @RequestBody PmgBatBuildingDynamicChangeStatusDto dto) {
        PmgBatBuildingDynamic byId = pmgBatBuildingDynamicService.getById(dto.getId());
        byId.setReleaseStatus(dto.getReleaseStatus());
        byId.setUpdateTime(new Date());
        return R.data(pmgBatBuildingDynamicService.updateById(byId));
    }

    //------------------------小程序用---------------------------------

    /**
     * 增加分享数
     * @tag pauly-lyz
     */
    @PutMapping("/increase/share/{id}")
    @ApiOperation(value = "增加分享数")
    public R increaseShare(@PathVariable Long id) {
        PmgBatBuildingDynamic byId = pmgBatBuildingDynamicService.getByIdForUpdate(id);
        byId.setShareNum(byId.getShareNum()+1);
        return R.data(pmgBatBuildingDynamicService.updateById(byId));
    }

}
