package cn.iocoder.yudao.module.system.controller.admin.rental;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.rentalsalerecord.RentalSaleRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.rentalsalerecord.RentalSaleRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.rentalsalerecord.RentalSaleRecordSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.rentalsalerecord.RentalSaleStatusReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.rental.RentalSaleRecordDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.rental.RentalSaleRecordService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;


@Tag(name = "管理后台 - 租赁转销售记录")
@RestController
@RequestMapping("/system/rental-sale-record")
@Validated
public class RentalSaleRecordController {

    @Resource
    private RentalSaleRecordService rentalSaleRecordService;

    @Resource
    private AdminUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建租赁转销售记录")
    public CommonResult<Long> createRentalSaleRecord(@Valid @RequestBody RentalSaleRecordSaveReqVO createReqVO) {
        return success(rentalSaleRecordService.createRentalSaleRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租赁转销售状态")
    @PreAuthorize("@ss.hasPermission('system:rental-sale-record:update')")
    public CommonResult<Boolean> updateRentalSaleRecord(@Valid @RequestBody RentalSaleStatusReqVO updateReqVO) {
        if (updateReqVO.getStatus() == 1) {
            updateReqVO.setReturnUserId(getLoginUserId());
        }
        rentalSaleRecordService.updateRentalSaleRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租赁转销售记录")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteRentalSaleRecord(@RequestParam("id") Long id) {
        rentalSaleRecordService.deleteRentalSaleRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租赁转销售记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<RentalSaleRecordRespVO> getRentalSaleRecord(@RequestParam("id") Long id) {
        RentalSaleRecordDO rentalSaleRecord = rentalSaleRecordService.getRentalSaleRecord(id);
        return success(BeanUtils.toBean(rentalSaleRecord, RentalSaleRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租赁转销售记录分页")
    public CommonResult<PageResult<RentalSaleRecordRespVO>> getRentalSaleRecordPage(@Valid RentalSaleRecordPageReqVO pageReqVO) {
        PageResult<RentalSaleRecordDO> pageResult = rentalSaleRecordService.getRentalSaleRecordPage(pageReqVO);
        PageResult<RentalSaleRecordRespVO> respVO = BeanUtils.toBean(pageResult, RentalSaleRecordRespVO.class);
        if (CollUtil.isNotEmpty(respVO.getList())) {
            // 获取退还经办人信息
            List<Long> userIds = convertList(respVO.getList(), RentalSaleRecordRespVO::getReturnUserId);
            Map<Long, AdminUserDO> userMap = userService.getUserMap(userIds);

            respVO.getList().forEach(item -> {
                // 设置退还经办人名称
                item.setReturnUserName(userMap.containsKey(item.getReturnUserId()) ? userMap.get(item.getReturnUserId()).getNickname() : "");
            });

        }
        return success(respVO);
    }
}