package cn.ysk.cashier.cons.service.impl;

import cn.ysk.cashier.cons.domain.TbConsInfo;
import cn.ysk.cashier.cons.domain.TbConsType;
import cn.ysk.cashier.cons.repository.TbConsInfoRepository;
import cn.ysk.cashier.utils.*;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.cons.repository.TbConsTypeRepository;
import cn.ysk.cashier.cons.service.TbConsTypeService;
import cn.ysk.cashier.cons.service.dto.TbConsTypeDto;
import cn.ysk.cashier.cons.service.dto.TbConsTypeQueryCriteria;
import cn.ysk.cashier.cons.service.mapstruct.TbConsTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import static cn.ysk.cashier.utils.StringCodeUtil.TYPE.LETTER_CAPITAL_NUMBER;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author admin
* @date 2024-06-22
**/
@Service
@RequiredArgsConstructor
public class TbConsTypeServiceImpl implements TbConsTypeService {

    private final TbConsTypeRepository tbConsTypeRepository;
    private final TbConsTypeMapper tbConsTypeMapper;

    private final TbConsInfoRepository tbConsInfoRepository;

    @Override
    public Map<String,Object> queryAll(TbConsTypeQueryCriteria criteria, Pageable pageable){
        Page<TbConsType> page = tbConsTypeRepository
                .findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);

        for (TbConsType tbConsType : page.getContent()) {
            tbConsType.setProductNum(tbConsTypeRepository.countProByTypeId(tbConsType.getId()));
        }

        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("content",page.getContent());
        map.put("totalElements",page.getTotalElements());

        return map;
    }

    @Override
    public List<TbConsTypeDto> queryAll(TbConsTypeQueryCriteria criteria){
        return tbConsTypeMapper.toDto(tbConsTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbConsTypeDto findById(Integer id) {
        TbConsType tbConsType = tbConsTypeRepository.findById(id).orElseGet(TbConsType::new);
        ValidationUtil.isNull(tbConsType.getId(),"TbConsType","id",id);
        return tbConsTypeMapper.toDto(tbConsType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbConsTypeDto create(TbConsType resources) throws Exception {


        resources.setConTypeCode(StringCodeUtil.getRandom(8,LETTER_CAPITAL_NUMBER));
        return tbConsTypeMapper.toDto(tbConsTypeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbConsType resources) {
        TbConsType tbConsType = tbConsTypeRepository.findById(resources.getId()).orElseGet(TbConsType::new);
        ValidationUtil.isNull( tbConsType.getId(),"TbConsType","id",resources.getId());
        tbConsType.copy(resources);
        tbConsTypeRepository.save(tbConsType);

        List<TbConsInfo> tbConsInfos=tbConsInfoRepository.findByConTypeId(tbConsType.getId());
        if(Objects.nonNull(tbConsInfos)){
             tbConsInfos.stream().forEach(it->{
                it.setConTypeName(resources.getConTypeName());
            });
        }

        tbConsInfoRepository.saveAll(tbConsInfos);

    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            tbConsTypeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TbConsTypeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbConsTypeDto tbConsType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("耗材类型代码", tbConsType.getConTypeCode());
            map.put("耗材名称", tbConsType.getConTypeName());
            map.put("状态 1 正常 0 禁用", tbConsType.getStatus());
            map.put("创建时间", tbConsType.getCreateTime());
            map.put("更新", tbConsType.getUpdateTime());
            map.put("店铺id", tbConsType.getShopId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
