package com.starry.admin.modules.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.modules.clerk.service.IPlayClerkUserInfoService;
import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
import com.starry.admin.modules.order.service.IPlayOrderInfoService;
import com.starry.admin.modules.platform.entity.SysTenantEntity;
import com.starry.admin.modules.platform.service.ISysTenantService;
import com.starry.admin.modules.platform.vo.SysTenantAddVo;
import com.starry.admin.modules.platform.vo.SysTenantQueryVo;
import com.starry.admin.modules.platform.vo.SysTenantReturnVo;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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


/**
 * 租户表Controller
 *
 * @author admin
 */
@RestController
@RequestMapping("/platform/tenant")
public class SysTenantController {
    @Resource
    private ISysTenantService sysTenantService;

    @Resource
    private IPlayClerkUserInfoService playClerkUserInfoService;

    @Resource
    private IPlayOrderInfoService playOrderInfoService;


    /**
     * 新增租户表
     */
    //@PreAuthorize("@customSs.hasPermission('platform:tenant:create')")
    @Log(title = "租户表", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@Validated @RequestBody SysTenantAddVo vo) {
        SysTenantEntity entity = ConvertUtil.entityToVo(vo, SysTenantEntity.class);
        return sysTenantService.addTenant(entity);
    }


    /**
     * 查询租户表列表
     */
    @PreAuthorize("@customSs.hasPermission('platform:tenant:list')")
    @PostMapping("/listByPage")
    public R listByPage(@RequestBody SysTenantQueryVo vo) {
        IPage<SysTenantEntity> page = sysTenantService.selectSysTenantList(vo);
        IPage<SysTenantReturnVo> voPage = page.convert(u -> {
            SysTenantReturnVo returnVo = new SysTenantReturnVo();
            List<PlayOrderInfoEntity> orderInfoEntities = playOrderInfoService.getTotalOrderInfo(u.getTenantId());
            BigDecimal rechargeAmount = BigDecimal.ZERO;
            for (PlayOrderInfoEntity orderInfoEntity : orderInfoEntities) {
                if ("0".equals(orderInfoEntity.getOrderType())) {
                    rechargeAmount = rechargeAmount.add(orderInfoEntity.getFinalAmount());
                }
            }

            BeanUtils.copyProperties(u, returnVo);
            returnVo.setClerkNumber(playClerkUserInfoService.getTotalClerkUser(u.getTenantId()));
            returnVo.setOrderNumber((long) orderInfoEntities.size());
            returnVo.setRechargeAmount(rechargeAmount);
            return returnVo;
        });


        return R.ok(voPage);
    }

    /**
     * 获取租户表详细信息
     */
    @PreAuthorize("@customSs.hasPermission('platform:tenant:query')")
    @GetMapping(value = "/{tenantId}")
    public R getInfo(@PathVariable("tenantId") String tenantId) {
        return R.ok(sysTenantService.selectSysTenantByTenantId(tenantId));
    }


    /**
     * 修改租户表
     */
    @PreAuthorize("@customSs.hasPermission('platform:tenant:edit')")
    @Log(title = "租户表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{tenantId}")
    public R update(@PathVariable String tenantId, @RequestBody SysTenantEntity sysTenantEntity) {
        return sysTenantService.updateTenant(sysTenantEntity);
    }

    /**
     * 删除租户表
     */
    @PreAuthorize("@customSs.hasPermission('platform:tenant:remove')")
    @Log(title = "租户表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tenantIds}")
    public R remove(@PathVariable String[] tenantIds) {
        return R.ok(sysTenantService.deleteSysTenantByTenantIds(tenantIds));
    }
}
