package lingtao.net.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import lingtao.net.bean.Product;
import lingtao.net.bean.ProductImg;
import lingtao.net.bean.QuoteData;
import lingtao.net.bean.QuoteLog;
import lingtao.net.bean.SysDictProduct;
import lingtao.net.bean.SysRole;
import lingtao.net.bean.SysUser;
import lingtao.net.dao.ProductImgMapper;
import lingtao.net.dao.QuoteDataMapper;
import lingtao.net.dao.QuoteLogMapper;
import lingtao.net.util.DateFormatUtils;
import lingtao.net.util.IPUtils;

@Service
public class QuoteLogService {

    @Autowired
    private QuoteLogMapper quoteLogMapper;

    @Autowired
    private QuoteDataMapper quoteDataMapper;

    @Autowired
    ProductImgMapper productImgMapper;

    @Autowired
    private SysRoleService sysRoleService;

    // 列表
    public List<QuoteLog> quoteLogs(QuoteLog quoteLog) {
        quoteLog.setQuoteTimeEnd(new DateFormatUtils().formatEndTime(quoteLog.getQuoteTimeBegin()));
        quoteLog.setQuoteTimeBegin(new DateFormatUtils().formatBeginTime(quoteLog.getQuoteTimeBegin()));
        return quoteLogMapper.quoteLogs(quoteLog);
    }

    // 操作日志
    public String log(Product product, HttpServletRequest request, List<Product> proList) {
        long startTime, endTime = 0;
        startTime = System.currentTimeMillis();
        SysUser user = (SysUser) request.getSession().getAttribute("USER_SESSION");
        if (user == null) {
            return "登陆失效";
        }
        QuoteLog log = new QuoteLog();
        ProductImg productImg = new ProductImg();

        // product转productImg
        BeanUtils.copyProperties(product, productImg);
        // 根据value查询label
        if ("13".equals(productImg.getProTypeValue()) || "15".equals(productImg.getProTypeValue())
                || "4".equals(productImg.getProTypeValue())) {
            productImg.setKind2Value(null);
        }
        // 优惠券
        if ("4".equals(productImg.getProTypeValue())) {
            productImg.setKindValue("0");
            productImg.setKind2Value(product.getKind());
        }
        // 合版封套
        if ("6".equals(productImg.getProTypeValue())) {
            if (product.getLengthSize() > 0) {
                productImg.setKind2Value("0");
            }
        }
        // 根据value获取label
        SysDictProduct label = productImgMapper.getLabel(productImg);

        if (StringUtils.isEmpty(label)) {
            return null;
        }
        if (proList.size() == 0) {
            return null;
        }
        String s = "";
        String remark_judge = "";
        String proTypeLabel = "";
        if ("常用种类".equals(product.getStickerKind())) {
            s += "常用种类- ";
        }
        if ("少数量".equals(product.getStickerKind())) {
            s += "少数量- ";
        }
        if ("专版打印".equals(product.getStickerKind())) {
            s += "专版打印- ";
        }
        if (!StringUtils.isEmpty(label.getProTypeLabel())) {
            if ("金属标".equals(label.getProTypeLabel()) && StringUtils.isEmpty(product.getCraftMo())) {
                s += "UV转印贴 - ";
                proTypeLabel = "UV转印贴";
            } else {
                s += label.getProTypeLabel() + " - ";
                proTypeLabel = label.getProTypeLabel();
            }
        }
        if ("少数量".equals(product.getCouponKind())) {
            s += product.getProTypeLabel() + " - ";
        }
        if (!StringUtils.isEmpty(label.getKindLabel())) {
            s += label.getKindLabel() + " - ";
        }
        if (!StringUtils.isEmpty(label.getKind2Label())) {
            s += label.getKind2Label() + " - ";
        }
        if (!StringUtils.isEmpty(product.getKind3Value())) {
            if ("0".equals(product.getKind3Value())) {
                s += "骑马钉 - ";
            }
        }
        String new_s = s;
        new_s += "客户旺旺:" + product.getWangwang() + " - ";
        // 产品 - 种类
        remark_judge = new_s;
        // 不区分专版打印或者常用，3分钟内都不要
        if (remark_judge.contains("常用种类- ") || remark_judge.contains("专版打印- ")) {
            remark_judge = remark_judge.substring(6);
        }
        // 画册P数
        if (!StringUtils.isEmpty(product.getPcount())) {
            s += product.getPcount() + "P - ";
        }
        if (!StringUtils.isEmpty(product.getSize())) {
            s += product.getSize() + " - ";
        }
        if (!StringUtils.isEmpty(product.getCraftMo())) {
            s += product.getCraftMo() + " - ";
        }
        if (!StringUtils.isEmpty(product.getCraftTang())) {
            s += product.getCraftTang() + " - ";
        }
        if (!StringUtils.isEmpty(product.getAotu())) {
            s += product.getAotu() + " - ";
        }
        if (!StringUtils.isEmpty(product.getYinbai())) {
            s += product.getYinbai() + " - ";
        }
        if (!StringUtils.isEmpty(product.getCraftSheng())) {
            s += product.getCraftSheng() + " - ";
        }
        if (!StringUtils.isEmpty(product.getCraftShua())) {
            s += product.getCraftShua() + " - ";
        }
        if (!StringUtils.isEmpty(product.getCraftBu())) {
            s += product.getCraftBu() + " - ";
        }
        if (!StringUtils.isEmpty(product.getCraftJiao())) {
            s += product.getCraftJiao() + " - ";
        }

        if (!StringUtils.isEmpty(product.getCraft())) {
            String[] craft = product.getCraft();
            for (int i = 0; i < craft.length; i++) {
                if (StringUtils.isEmpty(craft[i]))
                    continue;
                s += craft[i] + " - ";
            }
        }
        if (!StringUtils.isEmpty(product.getNumber())) {
            s += product.getNumber() + "款 - ";
        }

        if ("16".equals(product.getProTypeValue())) {
            s += product.getCount() + "本 " + proList.get(0).getPrice() + "元 ";
        } else {
            s += product.getCount() + "个 " + proList.get(0).getPrice() + "元 ";
        }

        log.setRemark(s);
        log.setRealname(user.getRealname());
        log.setUsername(user.getUsername());
        log.setQuoteIp(IPUtils.getIpAddr(request));// 获取ip
        log.setBrower(IPUtils.getBrowserName(request));// 获取浏览器名称
        log.setOs(IPUtils.getOsName(request)); // 获取操作系统名称
        log.setPrice(proList.get(0).getPrice());
        List<SysRole> allRoleNames = sysRoleService.getAllRoleName(null);
        String roleName = "";
        String[] split = user.getRole().split(",");
        for (SysRole sysRole : allRoleNames) {
            for (int i = 0; i < split.length; i++) {
                if (split[i].equals(String.valueOf(sysRole.getRoleId()))) {
                    roleName += sysRole.getRoleName() + "、";
                }
            }
        }
        log.setShopname(roleName);
        quoteLogMapper.insertSelective(log);

        endTime = System.currentTimeMillis();
        System.out.println("【insertSelective】使用的时间:" + (endTime - startTime));

        long startTime2, endTime2 = 0;
        startTime2 = System.currentTimeMillis();
        boolean flag = false;
        // 查询3分钟内自己报的数据
        List<String> remarkJudgeData = quoteDataMapper.getQuoteDataByMinutes(user.getUsername());
        endTime2 = System.currentTimeMillis();
        System.out.println("【getQuoteDataByMinutes】使用的时间:" + (endTime2 - startTime2));
        for (String remarkJudge : remarkJudgeData) {
            if (remarkJudge.equals(remark_judge)) {
                // 如果3分钟内报过相同数据
                flag = true;
                break;
            }
        }
        // 插入数据（给客服数据新增用的）
        String role = user.getRole();
        // 客服报的且3分钟内没有同一个客户报的数据才插入
        if (role.contains("999,") && !flag) {
            QuoteData quoteData = new QuoteData();
            quoteData.setUsername(user.getUsername());
            quoteData.setRealname(user.getRealname());
            quoteData.setPrice(proList.get(0).getPrice());
            quoteData.setRole(user.getRole());
            // 默认都是没选择店铺
            quoteData.setIsSelect("0");
//			role = role.replace("999,", "").replace(",1049", "").replace(",1011", "");
            // 规则:第一位客服，第二位主店铺
            String shopname = role.substring(role.indexOf(",") + 1);
            if (shopname.indexOf(",") != -1) {
                shopname = shopname.substring(0, shopname.indexOf(","));
            }
            System.out.println(shopname);
            quoteData.setShopname(shopname);
            quoteData.setIsSelect("1");
            // 除去【客服】只有一个店铺，所属店铺就是剩下这个店铺 切 选择状态改为已选择
//			if (!role.contains(",")) {
//			}
            quoteData.setIsBuy("0");
            quoteData.setIsBuyToDay("0");
            quoteData.setRemark(s);
            quoteData.setRemarkJudge(remark_judge);
            // 默认都是填写旺旺号
            quoteData.setIsFillIn("1");
            quoteData.setWangwang(product.getWangwang().trim());
            quoteData.setProTypeLabel(proTypeLabel);
            quoteDataMapper.addQuoteData(quoteData);
        }
        return null;
    }

    public String log(QuoteLog quoteLog, HttpServletRequest request) {
        long startTime, endTime = 0;
        startTime = System.currentTimeMillis();
        SysUser user = (SysUser) request.getSession().getAttribute("USER_SESSION");
        String remark_judge = quoteLog.getRemark();
        if (user == null) {
            return "登陆失效";
        }
        QuoteLog log = new QuoteLog();
        log.setRemark(remark_judge);
        log.setRealname(user.getRealname());
        log.setUsername(user.getUsername());
        log.setQuoteIp(IPUtils.getIpAddr(request));// 获取ip
        log.setBrower(IPUtils.getBrowserName(request));// 获取浏览器名称
        log.setOs(IPUtils.getOsName(request)); // 获取操作系统名称
        log.setPrice((double) 0);
        List<SysRole> allRoleNames = sysRoleService.getAllRoleName(null);
        String roleName = "";
        String[] split = user.getRole().split(",");
        for (SysRole sysRole : allRoleNames) {
            for (int i = 0; i < split.length; i++) {
                if (split[i].equals(String.valueOf(sysRole.getRoleId()))) {
                    roleName += sysRole.getRoleName() + "、";
                }
            }
        }
        log.setShopname(roleName);
        quoteLogMapper.insertSelective(log);

        endTime = System.currentTimeMillis();
        System.out.println("【insertSelective】使用的时间:" + (endTime - startTime));

        long startTime2, endTime2 = 0;
        startTime2 = System.currentTimeMillis();
        boolean flag = false;
        // 查询3分钟内自己报的数据
        List<String> remarkJudgeData = quoteDataMapper.getQuoteDataByMinutes(user.getUsername());
        endTime2 = System.currentTimeMillis();
        System.out.println("【getQuoteDataByMinutes】使用的时间:" + (endTime2 - startTime2));
        if (remarkJudgeData != null && remarkJudgeData.size() > 0) {
            for (String remarkJudge : remarkJudgeData) {
                if (remarkJudge != null && remarkJudge.equals(remark_judge)) {
                    // 如果3分钟内报过相同数据
                    flag = true;
                    break;
                }
            }
        }
        // 插入数据（给客服数据新增用的）
        String role = user.getRole();
        // 客服报的且3分钟内没有同一个客户报的数据才插入
        if (role.contains("999,") && !flag) {
            QuoteData quoteData = new QuoteData();
            quoteData.setUsername(user.getUsername());
            quoteData.setRealname(user.getRealname());
            quoteData.setPrice((double) 0);
            quoteData.setProTypeLabel("");
            quoteData.setRole(user.getRole());
            // 默认都是没选择店铺
            quoteData.setIsSelect("0");
//			role = role.replace("999,", "").replace(",1049", "").replace(",1011", "");
            // 规则:第一位客服，第二位主店铺
            String shopname = role.substring(role.indexOf(",") + 1);
            if (shopname.indexOf(",") != -1) {
                shopname = shopname.substring(0, shopname.indexOf(","));
            }
            System.out.println(shopname);
            quoteData.setShopname(shopname);
            quoteData.setIsSelect("1");
            // 除去【客服】只有一个店铺，所属店铺就是剩下这个店铺 切 选择状态改为已选择
//			if (!role.contains(",")) {
//			}
            quoteData.setIsBuy("0");
            quoteData.setIsBuyToDay("0");
            quoteData.setRemark(remark_judge);
            // 默认都是填写旺旺号
            quoteData.setIsFillIn("1");
            quoteDataMapper.addQuoteData(quoteData);
        }
        return null;
    }
}
