package com.ningdatech.kqapi.zzsfw.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.kqapi.zzsfw.entity.dto.NdKqZzsfwPolicyDTO;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMenu;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwPolicy;
import com.ningdatech.kqapi.zzsfw.entity.vo.NdKqZzsfwPolicyVO;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwPolicyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Classname PolicyManage
 * @Description
 * @Date 2023/11/02 14:55
 * @Author PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class PolicyManage {

    private final INdKqZzsfwPolicyService policyService;


    public PageVo<NdKqZzsfwPolicyVO> list(long pageNumber, long pageSize) {
        Page<NdKqZzsfwPolicy> page = Page.of(pageNumber,pageSize);
        Page<NdKqZzsfwPolicy> pageRes = policyService.page(page, Wrappers.lambdaQuery(NdKqZzsfwPolicy.class)
                .orderByAsc(NdKqZzsfwPolicy::getSort));

        if(CollUtil.isEmpty(pageRes.getRecords())){
            return PageVo.empty();
        }
        List<NdKqZzsfwPolicyVO> res = pageRes.getRecords().stream()
                .map(p -> BeanUtil.copyProperties(p,NdKqZzsfwPolicyVO.class))
                .collect(Collectors.toList());
        return PageVo.of(res,pageRes.getTotal());
    }

    public String save(NdKqZzsfwPolicyDTO dto) {
        if(Objects.isNull(dto)){
            return "保存失败 传入为空";
        }

        NdKqZzsfwPolicy entity = BeanUtil.copyProperties(dto,NdKqZzsfwPolicy.class);
        if(policyService.save(entity)){
            return "保存成功 :" + entity;
        }else{
            return "保存失败";
        }
    }

    public String removeAll() {
        policyService.remove(Wrappers.lambdaQuery(NdKqZzsfwPolicy.class));
        return "删除成功";
    }

    /**
     * 批量保存
     * @param dtos
     * @return
     */
    public String saveBatch(List<NdKqZzsfwPolicyDTO> dtos) {
        if(CollUtil.isEmpty(dtos)){
            return "保存失败 传入为空";
        }

        List<NdKqZzsfwPolicy> entitys = dtos.stream().map(d -> BeanUtil.copyProperties(d,NdKqZzsfwPolicy.class))
                .collect(Collectors.toList());
        if(policyService.saveBatch(entitys)){
                return "保存成功";
        }else{
            return "保存失败";
        }
    }
}
