package com.jeequan.jeepay.mgr.ctrl.merchant;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.PackageInfo;
import com.jeequan.jeepay.db.entity.PackageType;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.PackageInfoService;
import com.jeequan.jeepay.service.impl.PackageTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/packageType")
public class PackageTypeController extends CommonCtrl {

    @Autowired private PackageTypeService packageTypeService;
    @Autowired private PackageInfoService packageInfoService;

    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @MethodLog(remark = "方案分类列表")
    public ApiRes list(){
        IPage<PackageType> page=getIPage(true);
        PackageType packageType = getObject(PackageType.class);
        //提供id和名字检索
        LambdaQueryWrapper<PackageType> queryWrapper= Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotEmpty(packageType.getTypeName()),PackageType::getTypeName,packageType.getTypeName());
        queryWrapper.orderByDesc(PackageType::getCreatedAt);
        IPage<PackageType> result = packageTypeService.page(page,queryWrapper);
        return ApiRes.ok(result);
    }

    @MethodLog(remark = "新增方案分类")
    @PostMapping(value="/add")
    public ApiRes add() {
        PackageType packageType = getObject(PackageType.class);
        // 当前登录用户信息
        SysUser sysUser = getCurrentUser().getSysUser();
        packageType.setCreatedBy(String.valueOf(sysUser.getSysUserId()));
        packageTypeService.save(packageType);
        return ApiRes.ok();
    }

    @MethodLog(remark = "删除方案分类")
    @PostMapping(value="/deletePackage")
    public ApiRes deletePackage() {
        PackageType packageType = getObject(PackageType.class);
        //判断该节点下是否有子节点 有则不让删除
        LambdaQueryWrapper<PackageInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PackageInfo::getPackageId,packageType.getTypeId());
        wrapper.eq(PackageInfo::getPackageState,1);
        List<PackageInfo> packageInfosList = packageInfoService.list(wrapper);
        if (!packageInfosList.isEmpty()){
            return ApiRes.customFail("该方案分类下相关内容未清空！");
        }
        packageTypeService.removeById(packageType.getTypeId());
        return ApiRes.ok();
    }

    @MethodLog(remark = "更新方案分类信息")
    @PostMapping(value="/updatePackage")
    public ApiRes updatePackage() {
        //获取查询条件
        PackageType type = getObject(PackageType.class);
        type.setUpdatedAt(new Date());
        packageTypeService.updateById(type);
        return ApiRes.ok();
    }

    @RequestMapping(value = "/getTypeDetailById", method = RequestMethod.GET)
    @MethodLog(remark = "回显方案分类详情")
    public ApiRes getTypeDetailById() throws BizException {
        //获取查询条件
        PackageType packageType = getObject(PackageType.class);

        PackageType type  = packageTypeService.getById(packageType.getTypeId());
        return ApiRes.ok(type);
    }

}
