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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.mapping.finance.ModPriceLogMapper;
import com.th.demo.mapping.finance.VerifyAccountMapper;
import com.th.demo.mapping.ware.DistributionMapper;
import com.th.demo.mapping.ware.OutRecordMapper;
import com.th.demo.model.finance.ModPriceLog;
import com.th.demo.model.finance.VerifyAccount;
import com.th.demo.model.system.Type;
import com.th.demo.model.ware.Distribution;
import com.th.demo.model.ware.OutRecord;
import com.th.demo.model.ware.Store;
import com.th.demo.model.ware.TotalRecord;
import com.th.demo.service.ware.OutRecordService;
import com.th.demo.tools.JSONTools;
import com.th.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


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

@Service("OutRecordService")
public class OutRecordServiceImpl implements OutRecordService {

    @Autowired
    public OutRecordMapper outRecordMapper;
    @Autowired
    public VerifyAccountMapper verifyAccountMapper;
    @Autowired
    public DistributionMapper distributionMapper;

    @Autowired
    public ModPriceLogMapper modPriceLogMapper;

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

    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 carNo,
                        String tallyPeople, String ordNo, String fkComponyName, String contractNo, String packNo) {
        List<String> pltNos =new ArrayList<>() ;
        List<String> ordNos =new ArrayList<>() ;
        List<String> contractNos =new ArrayList<>() ;
        List<String> packNos =new ArrayList<>() ;
        if (plateNo.indexOf(",") != -1){
            pltNos = Arrays.asList(plateNo.split(","));
        }
        if (ordNo.indexOf(",") != -1){
            ordNos = Arrays.asList(ordNo.split(","));
        }
        if (contractNo.indexOf(",") != -1){
            contractNos = Arrays.asList(contractNo.split(","));
        }
        if (packNo.indexOf(",") != -1){
            packNos = Arrays.asList(packNo.split(","));
        }
        if(page == -1 && rows == -1){
            List<OutRecord> list=outRecordMapper.selectExpt(wareName,stackName,model,materialName,standard,  customerName,
                    plateNo, fromDate,  toDate,   userId, belongId,carNo, tallyPeople,ordNo,fkComponyName,
                    contractNo,packNo,pltNos,ordNos,contractNos,packNos);
            result = JSONTools.toStringyyyyMMddHHmmss(list);
        }else{
            PageHelper.startPage(page, rows);
            List<OutRecord> list=outRecordMapper.selectExpt(wareName,stackName,model,materialName,standard,  customerName,
                    plateNo, fromDate,  toDate,   userId, belongId,carNo,tallyPeople,ordNo,fkComponyName,
                    contractNo,packNo,pltNos,ordNos,contractNos,packNos);
            PageInfo<OutRecord> pageInfo = new PageInfo<>(list);
//        result = JSONTools.toString(pageInfo);
            result = JSONTools.toStringyyyyMMddHHmmss(pageInfo);
        }

        return result;
    }
    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 carNo,
                             String tallyPeople, String ordNo, String fkComponyName, String contractNo, String packNo) {
        List<String> pltNos =new ArrayList<>() ;
        List<String> ordNos =new ArrayList<>() ;
        List<String> contractNos =new ArrayList<>() ;
        List<String> packNos =new ArrayList<>() ;
        if (plateNo.indexOf(",") != -1){
            pltNos = Arrays.asList(plateNo.split(","));
        }
        if (ordNo.indexOf(",") != -1){
            ordNos = Arrays.asList(ordNo.split(","));
        }
        if (contractNo.indexOf(",") != -1){
            contractNos = Arrays.asList(contractNo.split(","));
        }
        if (packNo.indexOf(",") != -1){
            packNos = Arrays.asList(packNo.split(","));
        }
        List<TotalRecord> list=outRecordMapper.selectTotal(wareName,stackName,model,materialName,standard,  customerName,
                plateNo,fromDate,toDate,   userId, belongId,carNo,tallyPeople,ordNo,fkComponyName,
                contractNo,packNo,pltNos,ordNos,contractNos,packNos);
        result = JSONTools.toString(list);
        return result;
    }

    @Override
    public String saveVerifyAcc(String json, String accWgt, String accAmt, String invoiceAmt, String accDate, String accReminder, String userId, String belongId) {
        List<OutRecord> list = JSON.parseObject(json, new TypeReference<List<OutRecord>>() {
        });
        int num = 0;

        String leastRcdId = list.get(0).getId();
        double leastAmt = list.get(0).getWeight() * list.get(0).getInvoicePrice();

        OutRecord outRecord = new OutRecord();
        for (OutRecord rcd:list) {
            outRecord = outRecordMapper.selectByPrimaryKey(rcd.getId());
            if(outRecord.getVerifyAccountId() != null && outRecord.getVerifyAccountId().length() > 0){
                return JSONTools.toString(1,rcd.getPlateNo()+"已经对账，请勿重复对账");
            }
            if(rcd.getWeight() * rcd.getInvoicePrice() < leastAmt){
                leastRcdId = rcd.getId();
                leastAmt = rcd.getWeight() * rcd.getInvoicePrice();
            }
        }
        if(leastAmt < Double.valueOf(accReminder)){
            return JSONTools.toString(1,"扣零金额大于最小订单金额，无法操作");
        }
        VerifyAccount verifyAccount = new VerifyAccount();
        for (OutRecord rcd:list) {


            verifyAccount.setAccWgt(rcd.getWeight());
            verifyAccount.setAccAmt((rcd.getWeight() * rcd.getInvoicePrice()));
            double tempAmt = rcd.getWeight() * rcd.getInvoicePrice() - Double.valueOf(accReminder);
            if(leastRcdId == rcd.getId()){
                verifyAccount.setInvoiceAmt(tempAmt);
                verifyAccount.setAccReminder(Double.valueOf(accReminder));
            }else{
                verifyAccount.setInvoiceAmt(rcd.getWeight() * rcd.getInvoicePrice());
                verifyAccount.setAccReminder(Double.valueOf("0"));
            }
            verifyAccount.setAccDate(accDate);
            verifyAccount.setCrtTime(new Date());
            verifyAccount.setCrtUserId(userId);
            verifyAccount.setOutRcdId(rcd.getId());
            verifyAccount.setPlateNo(rcd.getPlateNo());
            verifyAccount.setOutDate(rcd.getAddTime());
            verifyAccount.setFkComponey(rcd.getFkComponyName());
            verifyAccount.setHqComponey(rcd.getCustomerName());
            verifyAccount.setOutCarNo(rcd.getOutCarNo());
            verifyAccount.setRcvAddress(rcd.getReceiveAddress());
            num = verifyAccountMapper.insert(verifyAccount);
            num += outRecordMapper.updateVerifyAccount(rcd.getId(),verifyAccount.getUuid());
        }
        return Tools.moreThanZeroResultJSON(num);
    }

    @Override
    public String savePriceMod(String json, String modPrice, String userId, String belongId) {
        List<OutRecord> list = JSON.parseObject(json, new TypeReference<List<OutRecord>>() {
        });
        ModPriceLog modPriceLog = new ModPriceLog();
        num = 0;
        int num1 = 0;
        for (OutRecord rcd:list) {
            modPriceLog.setCrtTime(new Date());
            modPriceLog.setCrtUserId(userId);
            modPriceLog.setOutRcdId(rcd.getId());
            modPriceLog.setPlateNo(rcd.getPlateNo());
            modPriceLog.setRawPrice(rcd.getInvoicePrice());
            modPriceLog.setModPrice(Double.valueOf(modPrice));
            num1 += modPriceLogMapper.writeLog(modPriceLog);

            rcd.setInvoicePrice(Double.valueOf(modPrice));
            num += outRecordMapper.savePriceMod(rcd);

        }
        if(num != num1){
            return JSONTools.toString(1,"日志写入错误，请重新操作");
        }
        return Tools.moreThanZeroResultJSON(num);
    }

    @Override
    public String saveCar(String json,String truckNo,String driverName,String driverPhone,String userId,String belongId) {
        List<OutRecord> list = JSON.parseObject(json, new TypeReference<List<OutRecord>>() {
        });
        Distribution distribution = distributionMapper.selectByPrimaryKey(list.get(0).getDistributionId());
        distribution.setTruckNo(truckNo);
        distribution.setDriverName(driverName);
        distribution.setDriverPhone(driverPhone);
        num = distributionMapper.updateByPrimaryKey(distribution);

        return Tools.moreThanZeroResultJSON(num);
    }
}


















