package com.hfkj.service.user.impl;

import com.hfkj.common.obs.HuaWeiYunObs;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.config.CommonSysConst;
import com.hfkj.dao.*;
import com.hfkj.entity.*;
import com.hfkj.service.user.PhgDataUserService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

@Service("phgDataUserService")
public class PhgDataUserServiceImpl implements PhgDataUserService {

    @Resource
    private CyymallUserMapper cyymallUserMapper;

    @Resource
    private CyymallOrderMapper cyymallOrderMapper;

    @Resource
    private CyymallOrderDetailMapper cyymallOrderDetailMapper;

    @Resource
    private CyymallAddressMapper cyymallAddressMapper;

    @Resource
    private CyymallGoodsMapper cyymallGoodsMapper;

    @Resource
    private BsOrderMapper orderMapper;
    @Qualifier("bsOrderMapperExt")

    @Resource
    private BsOrderMapperExt bsOrderMapperExt;

    @Override
    public CyymallUser queryDetail(Integer id) {
        return cyymallUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CyymallUser> getList(Map<String, Object> map) {

        CyymallUserExample example = new CyymallUserExample();
        CyymallUserExample.Criteria criteria = example.createCriteria();


        if (MapUtils.getShort(map, "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getShort(map, "type"));
        }
        return cyymallUserMapper.selectByExample(example);
    }

    @Override
    public List<CyymallOrder> getListOrder(Map<String, Object> map) {

        CyymallOrderExample example = new CyymallOrderExample();
        CyymallOrderExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "userId") != null) {
            criteria.andUserIdEqualTo(MapUtils.getInteger(map, "type"));
        }
        if (MapUtils.getShort(map, "isPay") != null) {
            criteria.andIsPayEqualTo(MapUtils.getShort(map, "isPay"));
        }
        if (MapUtils.getShort(map, "applyDelete") != null) {
            criteria.andApplyDeleteEqualTo(MapUtils.getShort(map, "applyDelete"));
        }
        return cyymallOrderMapper.selectByExample(example);
    }

    @Override
    public List<CyymallOrderDetail> getListOrderDetail() {
        return cyymallOrderDetailMapper.selectByExample(new CyymallOrderDetailExample());
    }

    @Override
    public CyymallOrderDetail getDetail(Integer id) {
        return cyymallOrderDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CyymallAddress> getAddressList() {
        CyymallAddressExample example = new CyymallAddressExample();
        CyymallAddressExample.Criteria criteria = example.createCriteria();

        return cyymallAddressMapper.selectByExample(example);
    }


    @Override
    public CyymallGoods findGoods(Integer id) {
        return cyymallGoodsMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<CyymallGoods> getListGoods() {
        return cyymallGoodsMapper.selectByExample(new CyymallGoodsExample());
    }


    @Override
    public void insertOrderBatch(List<BsOrder> list) {
        bsOrderMapperExt.insertOrderBatch(list);
    }

    @Override
    public void insertOrderChildBatch(List<BsOrderChild> list) {
        bsOrderMapperExt.insertOrderChildBatch(list);
    }

    @Override
    public void insertOrderCouponBatch(List<BsOrderCoupon> list) {
        bsOrderMapperExt.insertOrderCouponBatch(list);
    }

    @Override
    public void insertOrderCouponNoBatch(List<BsOrderCouponNo> list) {
        bsOrderMapperExt.insertOrderCouponNoBatch(list);
    }

    @Override
    public void insertOrderGoodsBatch(List<BsOrderGoods> list) {
        bsOrderMapperExt.insertOrderGoodsBatch(list);
    }

    @Override
    public void insertOrderDeductionBatch(List<BsOrderDeduction> list) {
        bsOrderMapperExt.insertOrderDeductionBatch(list);
    }

    @Override
    public String downloadImage(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);


            // 创建图片路径
            String fileName = System.currentTimeMillis() + ".png";
            String childPath = DateUtil.date2String(new Date(), "yyyyMM");
            String destDirName = CommonSysConst.getSysConfig().getFileUrl() + File.separator + childPath;
            // 创建文件夹
            File dir = new File(destDirName);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            FileOutputStream fileOutputStream = new FileOutputStream(destDirName + File.separator + fileName);

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
                fileOutputStream.flush();
            }
            HuaWeiYunObs.putObject(CommonSysConst.getSysConfig().getObsBucketName(),
                    childPath + "/" + fileName, new File(destDirName + "/" + fileName));
            fileOutputStream.close();
            bufferedInputStream.close();
            inputStream.close();

            return childPath + "/" + fileName;
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return null;

    }
}
