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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.th.demo.mapping.finance.AccountDetailMapper;
import com.th.demo.mapping.maint.PriceMapper;
import com.th.demo.mapping.maint.StackMapper;
import com.th.demo.mapping.ware.ChangeRecordMapper;
import com.th.demo.mapping.ware.CombineRecordMapper;
import com.th.demo.mapping.ware.StoreMapper;
import com.th.demo.model.finance.AccountDetail;
import com.th.demo.model.maint.Customer;
import com.th.demo.model.maint.Price;
import com.th.demo.model.maint.Stack;
import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.SysUser;
import com.th.demo.model.system.Type;
import com.th.demo.model.ware.*;
import com.th.demo.service.ware.ChangeService;
import com.th.demo.service.ware.CombineService;
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 org.springframework.transaction.annotation.Transactional;

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

@Service("ChangeService")
public class ChangeServiceImpl implements ChangeService {
    @Autowired
    public AccountDetailMapper accountDetailMapper;
    @Autowired
    public ChangeRecordMapper changeRecordMapper;
    @Autowired
    public PriceMapper priceMapper;
    @Autowired
    public StoreMapper storeMapper;

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


    @Transactional(rollbackFor = Exception.class)
    @Override
    public String changeCustomer(String json, String customer,String address,String userId, String belongId) throws Exception{
        List<Store> list = JSON.parseObject(json, new TypeReference<List<Store>>() {
        });
        Customer c = JSON.parseObject(customer, new TypeReference<Customer>() {
        });
        String recordType = "";
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getCustomer().getName().equals(c.getName())){
                recordType = "0";
            }else {
                recordType = "1";
                AccountDetail accountDetail = getAccountDetail(list.get(i),  userId, belongId);
                num += accountDetailMapper.insert(accountDetail);
            }
            ChangeRecord changeRecord = getChangeRecord(recordType, list.get(i), c, address, userId, belongId);
            num += changeRecordMapper.insert(changeRecord);


            list.get(i).setCustomer(c);
            list.get(i).setReceiveAddress(address);
            num += storeMapper.updateByPrimaryKey(list.get(i));
        }
        return Tools.moreThanZeroResultJSON(num);
    }

    private ChangeRecord getChangeRecord(String recordType, Store store, Customer customer, String receiveAddress, String userId, String belongId) {

        ChangeRecord record = new ChangeRecord();
        record.setRecordType(recordType);
        record.setWareName(store.getWare().getName());
        record.setStackName(store.getStack().getName());
        record.setLayer(store.getLayer());
        record.setMaterialName(store.getMaterial().getName());
        record.setStandard(store.getMaterial().getStandard());
        record.setModel(store.getModel());
        if(customer==null) {
            record.setCustomerNameNew("");
        }else {
            record.setCustomerNameNew(customer.getName());
        }
        record.setCustomerNameOld(store.getCustomer().getName());
        record.setCount(store.getCount());
        record.setWeight(store.getWeight());
        record.setRemark(store.getRemark());
        record.setBelongId(store.getBelongId());
        record.setPlateNo(store.getPlateNo());
        record.setAddUser(new SysUser(userId));
        record.setAddTime(new Date());
        record.setInId(store.getInId());
        record.setReceiveAddressOld(store.getReceiveAddress());
        record.setReceiveAddressNew(receiveAddress);
        record.setCancelFlag("0");
        return record;
    }

    private AccountDetail getAccountDetail(Store store,  String userId, String belongId) throws Exception {
        int changeTime = 0;
        int changeTimeForPrice = 0;
        Double plateSuttle = 0.0;
        Double storagePrice = 0.0;
        Double weightPrice = 0.0;

        List<ChangeRecord> list = changeRecordMapper.selectByPlantNoForAccount(store.getPlateNo(), belongId);
        if (list == null || list.size() == 0) {
            changeTime = 0;
            changeTimeForPrice = 0;
        } else {
            changeTime = list.size();
            changeTimeForPrice = 1;
        }
       /* Price price = priceMapper.selectByMaterialChangeTime(store.getMaterial().getId(), changeTimeForPrice, belongId);
        if (price == null) {
            throw new Exception(store.getMaterial().getName() + "," + store.getMaterial().getStandard() + "没有对应价格信息");
        }*/
        AccountDetail accountDetail = new AccountDetail();
        accountDetail.setChangeTime(changeTime);
        accountDetail.setRecordType("货权转移");
        accountDetail.setPlateNo(store.getPlateNo());
        accountDetail.setWareName(store.getWare().getName());
        accountDetail.setStackName(store.getStack().getName());
        accountDetail.setLayer(store.getLayer());
        accountDetail.setMaterialName(store.getMaterial().getName());
        accountDetail.setStandard(store.getMaterial().getStandard());
        accountDetail.setModel(store.getModel());
        accountDetail.setCustomerName(store.getCustomer().getName());
        accountDetail.setCount(store.getCount());
        accountDetail.setWeight(store.getWeight());
        accountDetail.setReceiveAddress(store.getReceiveAddress());
        accountDetail.setAccountFlag("0");
        accountDetail.setAddUser(new SysUser(userId));
        accountDetail.setAddTime(new Date());
        accountDetail.setCancelFlag("0");
        accountDetail.setBelongId(belongId);

        //storagePrice = price.getStoragePrice() * store.getWeight();

        //accountDetail.setStorageUnitPrice(price.getStoragePrice());
        accountDetail.setStoragePrice(storagePrice);

        //accountDetail.setWeightUnitPrice(price.getWeightPrice());
        accountDetail.setWeightPrice(0.0);
        accountDetail.setSuttle(0.0);

        accountDetail.setReduceUnitPrice(0.0);
        accountDetail.setReducePrice(0.0);

        accountDetail.setTotalPrice(storagePrice+weightPrice);


        return accountDetail;
    }

}
