package com.hzs.sale.wares.provider;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.domain.R;
import com.hzs.common.domain.sale.ext.BdWaresInfoExt;
import com.hzs.common.domain.sale.wares.BdWares;
import com.hzs.common.domain.sale.wares.BdWaresLabel;
import com.hzs.sale.wares.IWaresServiceApi;
import com.hzs.sale.wares.dto.WaresDTO;
import com.hzs.sale.wares.service.IBdWaresLabelService;
import com.hzs.sale.wares.service.IBdWaresService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

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

@DubboService
public class WaresServiceProvider implements IWaresServiceApi {

    @Autowired
    private IBdWaresService iBdWaresService;
    @Autowired
    private IBdWaresLabelService iBdWaresLabelService;

    @Override
    public R<Boolean> checkLabelisUse(Integer pkLabel) {
        LambdaQueryWrapper<BdWaresLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdWaresLabel::getPkLable, pkLabel);
        if (iBdWaresLabelService.count(queryWrapper) > 0) {
            return R.ok(true);
        } else {
            return R.fail(false);
        }
    }

    @Override
    public void updateByAutoStartAndAutoStart(Date currentTime) {
        iBdWaresService.updateByAutoStartAndAutoStart(currentTime);
    }

    @Override
    public void updateByAutoStartAndAutoEnd(Date currentTime) {
        iBdWaresService.updateByAutoStartAndAutoEnd(currentTime);
    }

    @Override
    public R<BdWaresInfoExt> selectWaresBySpecial(Integer specialArea, Integer pkCountry) {
        return R.ok(iBdWaresService.selectWaresBySpecial(specialArea, pkCountry));
    }

    @Override
    public R<List<WaresDTO>> findAll(Integer pkCountry) {
        LambdaQueryWrapper<BdWares> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdWares::getPkCountry, pkCountry);
        List<BdWares> list = iBdWaresService.list(queryWrapper);
        List<WaresDTO> waresDTOList = list.stream().map(a -> BeanUtil.copyProperties(a, WaresDTO.class)).collect(Collectors.toList());
        return R.ok(waresDTOList);
    }

}
