package com.hys.app.controller.erp.use;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.converter.erp.CompanyPackageConverter;
import com.hys.app.framework.database.BaseQueryParam;
import com.hys.app.model.erp.entity.*;
import com.hys.app.model.support.LogClient;
import com.hys.app.model.support.validator.annotation.Log;
import com.hys.app.service.erp.CompanyPackageManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "套餐")
@RestController
@RequestMapping("/admin/erp/companyPackage")
@AllArgsConstructor
public class CompanyPackageController {

    private CompanyPackageManager companyPackageManager;

    @PostMapping("createCompanyPackage")
    @ApiOperation(value = "创建套餐")
    @Log(client = LogClient.admin, detail = "创建名称为[${companyDO.name}]的公司信息")
    public Long createCompany(@Valid @RequestBody CompanyPackageDO packageDo) {
        return companyPackageManager.createCompanyPackage(packageDo);
    }

    @PostMapping("updateCompanyPackage/{id}")
    @ApiOperation(value = "修改套餐")
    @Log(client = LogClient.admin, detail = "创建名称为[${companyDO.name}]的公司信息")
    public Long updateCompanyPackage(@Valid @RequestBody CompanyPackageDO packageDo, @PathVariable Long id) {
        return companyPackageManager.updateCompanyPackage(packageDo,id);
    }

    @PostMapping("disAbleUse/{id}")
    @ApiOperation(value = "套餐启用禁用")
    @Log(client = LogClient.admin, detail = "修改id为[${id}]套餐的状态")
    public void disAbleUse(@PathVariable Long id) {
        CompanyPackage byId = companyPackageManager.getById(id);
        byId.setUseAble(!byId.getUseAble());
        companyPackageManager.updateById(byId);
    }

    @GetMapping("{id}")
    @ApiOperation(value = "获取套餐详情")
    public CompanyPackageVO getPackage(@PathVariable Long id) {
        return CompanyPackageConverter.INSTANCE.convert(companyPackageManager.getById(id));
    }

    @PostMapping()
    @ApiOperation(value = "获取套餐列表")
    public IPage<CompanyPackageVO> getPackageList(@RequestBody CompanyPackageQuery packageDO) {
        return CompanyPackageConverter.INSTANCE.convert(companyPackageManager.page(new Page<>(packageDO.getPageNo(), packageDO.getPageSize()),
                new LambdaQueryWrapper<CompanyPackage>().eq(ObjectUtil.isNotNull(packageDO.getUseAble()), CompanyPackage::getUseAble, packageDO.getUseAble())
                        .like(StrUtil.isNotBlank(packageDO.getName()), CompanyPackage::getName, packageDO.getName()).orderByDesc(CompanyPackage::getUseAble,CompanyPackage::getCreateTime)));
    }
}
