package com.th.demo.service.impl.customer;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.mapping.ware.StoreMapper;
import com.th.demo.model.ware.Store;
import com.th.demo.model.ware.TotalRecord;
import com.th.demo.service.customer.StoreQueryService;
import com.th.demo.tools.DateTools;
import com.th.demo.tools.JSONTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("StoreQueryService")
public class StoreQueryServiceImpl implements StoreQueryService {

    @Autowired
    StoreMapper storeMapper;
    String result = "";

    @Override
    public String query(int page, int rows, String wareName, String stackName, String model, String materialName, String standard, String customerName, String plateNo, String beginDate,
                        String endDate, String userId, String belongId, String ordNo) {

        if (beginDate == null || beginDate.equals("")) {

        } else {
            beginDate = beginDate.substring(0, 10);
            Date begin = DateTools.StrToDate(beginDate, "yyyy-MM-dd");
            endDate = endDate.substring(0, 10);
            Date end = DateTools.StrToDate(endDate, "yyyy-MM-dd");
        }
        if(page == -1 && rows == -1){
            List<Store> list = storeMapper.selectForCustomer(wareName,
                    stackName,
                    model,
                    materialName,
                    standard,
                    customerName,
                    plateNo,
                    beginDate,
                    endDate,
                    userId
                    ,ordNo);
            result = JSONTools.toStringyyyyMMddHHmmss(list);
        }else{
            PageHelper.startPage(page, rows);
            List<Store> list = storeMapper.selectForCustomer(wareName,
                    stackName,
                    model,
                    materialName,
                    standard,
                    customerName,
                    plateNo,
                    beginDate,
                    endDate,
                    userId
                    ,ordNo);
            PageInfo<Store> pageInfo = new PageInfo<>(list);
            result = JSONTools.toStringyyyyMMddHHmmss(pageInfo);
        }

        return result;
    }

    @Override
    public String queryTotal(int page, int rows, String wareName, String stackName, String model, String materialName, String standard, String customerName, String plateNo, String beginDate, String endDate, String userId, String belongId, String ordNo) {
        if (beginDate == null || beginDate.equals("")) {

        } else {
            beginDate = beginDate.substring(0, 10);
            Date begin = DateTools.StrToDate(beginDate, "yyyy-MM-dd");
            endDate = endDate.substring(0, 10);
            Date end = DateTools.StrToDate(endDate, "yyyy-MM-dd");
        }
        List<TotalRecord> list = storeMapper.queryTotal(wareName,
                stackName,
                model,
                materialName,
                standard,
                customerName,
                plateNo,
                beginDate,
                endDate,
                userId
                ,ordNo);
        result = JSONTools.toStringyyyyMMddHHmmss(list);
        return result;
    }
}
