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

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.mapping.ware.InRecordMapper;
import com.th.demo.model.system.Type;
import com.th.demo.model.ware.InRecord;
import com.th.demo.model.ware.TotalRecord;
import com.th.demo.service.ware.InRecordService;
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("InRecordService")
public class InRecordServiceImpl implements InRecordService {

    @Autowired
    public InRecordMapper inRecordMapper;

    String result = Type.FAIL;
    int num = 0;

    @Override
    public String query(int page, int rows, String wareName, String stackName, String model, String materialName, String standard, String customerName,
                        String plateNo, Date fromDate,
                        Date toDate, String userId, String belongId, String ordNo, String carNo, String tallyPeople, String fkComponyName,String contractNo) {
        if(page == -1 && rows == -1){
            List<InRecord> list=inRecordMapper.select(wareName,stackName,model,materialName,standard,    customerName,
                    plateNo, fromDate,toDate , userId, belongId,ordNo,carNo,tallyPeople,fkComponyName,contractNo);;
            result = JSONTools.toStringyyyyMMddHHmmss(list);
        }else{
            PageHelper.startPage(page, rows);

            List<InRecord> list=inRecordMapper.select(wareName,stackName,model,materialName,standard,    customerName,
                    plateNo, fromDate,toDate , userId, belongId, ordNo, carNo,tallyPeople,fkComponyName,contractNo);

            PageInfo<InRecord> pageInfo = new PageInfo<>(list);
            result = JSONTools.toStringyyyyMMddHHmmss(pageInfo);
        }

        return result;
    }

    @Override
    public String updateIn(String json, String userId, String belongId) {
        InRecord inRecord = JSON.parseObject(json, InRecord.class);
        InRecord isnull =inRecordMapper.selectByPrimaryKey(inRecord.getId());
        if(isnull==null ){
            num = inRecordMapper.updateIn(inRecord);
            result = JSONTools.toString(num);
        }else {
            num = inRecordMapper.updateByPrimaryKey(inRecord);
            result = JSONTools.toString(num);
        }
        return null;
    }
    @Override
    public String queryTotal(int page, int rows, String wareName, String stackName, String model, String materialName, String standard, String customerName,
                             String plateNo, Date fromDate, Date toDate, String userId, String belongId, String ordNo, String carNo, String tallyPeople, String fkComponyName) {
        List<TotalRecord> list=inRecordMapper.selectTotal(wareName,stackName,model,materialName,standard,    customerName,
                plateNo,fromDate,toDate,   userId, belongId,ordNo,carNo,tallyPeople,fkComponyName);
        result = JSONTools.toStringyyyyMMddHHmmss(list);
        return result;
    }
}
