package com.iamberry.wechat.service.machineNumber;

import com.iamberry.wechat.core.entity.Page;
import com.iamberry.wechat.core.entity.PaperBean;
import com.iamberry.wechat.core.entity.cart.ShopOrderAndItemDto;
import com.iamberry.wechat.core.entity.machineNumber.MachineGift;
import com.iamberry.wechat.core.entity.machineNumber.MachineNumber;
import com.iamberry.wechat.core.entity.machineNumber.MachineNumberLogs;
import com.iamberry.wechat.core.entity.machineNumber.WarrantyCard;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.order.Order;
import com.iamberry.wechat.core.entity.order.OrderItem;
import com.iamberry.wechat.core.entity.wx.QRCJson;
import com.iamberry.wechat.face.machineNumber.MachineNumberService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.service.cart.CartServiceImpl;
import com.iamberry.wechat.service.cart.dao.CartDao;
import com.iamberry.wechat.service.mapper.MachineNumberMapper;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.OrderNOUtil;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.WeixinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Created by Administrator on 2017/7/20.
 */
@Service
public class MachineNumberServiceImpl implements MachineNumberService{
    @Autowired
    private MachineNumberMapper machineNumberMapper;
    @Autowired
    private MemberService memberService;
    @Autowired
    private CartDao cartDao;
    @Override
    public Integer addNumberLogs(MachineNumberLogs machineNumberLogs) {
        return machineNumberMapper.addNumberLogs(machineNumberLogs);
    }

    @Override
    public Integer addMachineNumber(MachineNumber machineNumber) {
        return machineNumberMapper.addMachineNumber(machineNumber);
    }

    @Override
    public List<MachineNumberLogs> listNumberLogs(Page page) {
        return machineNumberMapper.listNumberLogs(page);
    }

    @Override
    public Integer listNumberLogsCount(Page page) {
        return machineNumberMapper.listNumberLogsCount(page);
    }

    @Override
    public List<MachineNumber> listNumberQrCode(Page page) {
        return machineNumberMapper.listNumberQrCode(page);
    }

    @Override
    public Integer listNumberQrCodeCount(Page page) {
        return machineNumberMapper.listNumberQrCodeCount(page);
    }

    @Override
    public Integer updateIsprInt(String numberId) {
        return machineNumberMapper.updateIsprInt(numberId);
    }

    @Override
    public Integer selectQrcodeLogsByBatch(String logsBatch) {
        return machineNumberMapper.selectQrcodeLogsByBatch(logsBatch);
    }

    @Override
    public List<MachineNumber> listMachineNumber(MachineNumber machineNumber) {
        return machineNumberMapper.listMachineNumber(machineNumber);
    }

    @Override
    public List<WarrantyCard> listWarrantyCard(WarrantyCard warrantyCard) {
        return machineNumberMapper.listWarrantyCard(warrantyCard);
    }

    @Override
    public Integer WarrantyCardCount(WarrantyCard warrantyCard) {
        int count = machineNumberMapper.WarrantyCardCount(warrantyCard);
        return	count = count % warrantyCard.getPage().getPageSize() == 0? count/warrantyCard.getPage().getPageSize():(count/warrantyCard.getPage().getPageSize())+1;
    }

    @Override
    public boolean updateUserAndNumber(Member member, MachineNumber machineNumber) {
        boolean flag = false;
        //修改用户信息
        int num = memberService.updateMemberByUserId(member);
        //修改机器编号信息
        int sum = machineNumberMapper.updateMachineNumber(machineNumber);

        MachineGift machineGift = new MachineGift();
        machineGift.setGiftType(1);

        if (num > 0 && sum > 0) {
            flag = true;
        }
        return flag;
    }


    @Override
    public boolean addUserAndNumber(Member member, MachineNumber machineNumber) {
        boolean flag = false;
        //修改用户信息
        int num = memberService.updateMemberByUserId(member);
        /*//修改机器编号信息
        int sum = machineNumberMapper.updateMachineNumber(machineNumber);*/
        //新增机器信息，并加入用户信息
        machineNumber.setNumberQrcode("http://static.iamberry.com/tooth/images/system_qrcode.jpg");//默认二维码
        //生成条形码
        String berQrcode = String.valueOf(new Date().getTime());
        machineNumber.setNumberBarcode(berQrcode+"0");
        machineNumber.setNumberBatch("000000");//批次默认为6个0
        int sum = machineNumberMapper.addMachineNumberAndUser(machineNumber);
        if (num > 0 && sum > 0) {
            flag = true;
        }
        return flag;
    }


    @Override
    public boolean updateNumber(MachineNumber machineNumber) {
        //修改机器编号信息
        int sum = machineNumberMapper.updateMachineNumber(machineNumber);
        if (sum > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<MachineNumberLogs> qrcodProgressUpdate(String[] logsBatchs) {
        return machineNumberMapper.qrcodProgressUpdate(logsBatchs);
    }

    @Override
    public Integer addMachineNumberAndUser(MachineNumber machineNumber) {
        return machineNumberMapper.addMachineNumberAndUser(machineNumber);
    }

    @Override
    public boolean addMachineNumberList(List<MachineNumber> list, String openid, Member member) {
        boolean flag = false;
        boolean isflag = true;//判断是否需要进入循环
        int sum = 0;
        String qrcode = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";//暂时不用二维码
        MachineNumber machineNumber = new MachineNumber();
        machineNumber.setNumberOpenId(openid);
        machineNumber.setPage(null);
        List<MachineNumber> numberList = new ArrayList<MachineNumber>();

        if (list == null || list.size() <= 0) {
            return flag;
        }

        //生成条形码
        String userid = member.getUserId().toString();
        int len = 11;//条形码不加sum的定长
        StringBuilder berQrcode = new StringBuilder(userid);
        String dateTime = String.valueOf(new Date().getTime());
        int dateTimeLen = len - userid.length();
        dateTime = dateTime.substring(0,dateTimeLen);
        berQrcode.append(dateTime);

        //根据条形码查询该机器信息
        List<MachineNumber> machineList = machineNumberMapper.listMachineNumber(machineNumber);
        if (machineList == null || machineList.size() == 0) {
            numberList = list;
            isflag = false;
        }
        if (isflag) {
            //循环筛选出还未添加的机器信息
            for (MachineNumber machine : list) {
                int num = 0;
                sum++;
                for (MachineNumber number : machineList) {
                    if (machine.getNumberSalesTime().getTime() == number.getNumberSalesTime().getTime()) {
                        num++;
                    }
                }
                if (num == 0) {

                    //生成机器二维码
                    /*String json = String.format(NameUtils.QR_LIMIT_STR_SCENE, ResultInfo.barCodePrefix + berQrcode + sum);
                    QRCJson qrc = WeixinUtil.createQrcode(json);*/
                    //不需要用到二维码，所以给默认值
                    machine.setNumberQrcode(qrcode);
                    machine.setNumberBarcode(berQrcode.toString() + sum);
                    machine.setNumberBatch(berQrcode.toString());
                    machine.setNumberStatus(1);
                    numberList.add(machine);
                }
            }
        } else {
            for (MachineNumber machine : numberList) {
                sum++;
                //生成机器二维码
                /*String json = String.format(NameUtils.QR_LIMIT_STR_SCENE, ResultInfo.barCodePrefix + berQrcode + sum);
                QRCJson qrc = WeixinUtil.createQrcode(json);*/
                machine.setNumberQrcode(qrcode);
                machine.setNumberBarcode(berQrcode.toString() + sum);
                machine.setNumberBatch(berQrcode.toString());
                machine.setNumberStatus(1);
            }
        }

        //修改用户信息
        int num = memberService.updateMemberByUserId(member);
        //批量添加
        int count = 1;
        if (numberList.size() > 0) {
            count = machineNumberMapper.addMachineNumberList(numberList);
        }
        if (num > 0 && count > 0) {
            flag = true;
        } else {
            flag = false;
        }

        return flag;
    }
}
