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

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.AccountDetailMapper;
import com.th.demo.model.finance.AccountDetail;
import com.th.demo.model.finance.RcvMoney;
import com.th.demo.model.ware.InRecord;
import com.th.demo.model.ware.Store;
import com.th.demo.service.finance.AccountDetailService;
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.Date;
import java.util.List;

@Service("AccountDetailService")
public class AccountDetailServiceImpl implements AccountDetailService {

    @Autowired
    AccountDetailMapper accountDetailMapper;
    String result = "";

    int num = 0;

    @Override
    public String query(int page, int rows, String customerName, String accountFlag, String userId, String belongId) {
        PageHelper.startPage(page, rows);
        List<AccountDetail> list = accountDetailMapper.select(customerName, accountFlag, belongId);
        PageInfo<AccountDetail> pageInfo = new PageInfo<>(list);
        result = JSONTools.toStringyyyyMMddHHmmss(pageInfo);
        return result;
    }

    @Override
    public String modifyReduce(String json, double reduceUnitPrice, String userId, String belongId) {
        num = 0;
        List<AccountDetail> list = JSON.parseObject(json, new TypeReference<List<AccountDetail>>() {
        });
        Double reducePrice = 0.0;
        AccountDetail accountDetail = null;
        for (int i = 0; i < list.size(); i++) {
            accountDetail = list.get(i);
            if (accountDetail.getSuttle() == 0.0) {
                reducePrice = reduceUnitPrice * accountDetail.getWeight();
            } else {
                reducePrice = reduceUnitPrice * accountDetail.getSuttle();
            }
            accountDetail.setReduceUnitPrice(reduceUnitPrice);
            accountDetail.setReducePrice(reducePrice);
            accountDetail.setTotalPrice(accountDetail.getStoragePrice()+accountDetail.getWeightPrice()+reducePrice);
            accountDetail.setModifyId(userId);
            accountDetail.setModifyTime(new Date());
            num += accountDetailMapper.updateByPrimaryKey(accountDetail);
        }
        return Tools.moreThanZeroResultJSON(num);
    }

    @Override
    public String queryBycustomer(String customerName, String accountFlag, String userId, String belongId) {
        List<AccountDetail> list = accountDetailMapper.queryBycustomer(customerName, accountFlag, belongId);
        result = JSONTools.toStringyyyyMMddHHmmss(list);
        return result;
    }

    @Override
    public String queryRcvMoney(String fromDate, String toDate) {
        List<RcvMoney> list = accountDetailMapper.queryRcvMoneyAmt(fromDate, toDate);
        result = JSONTools.toStringyyyyMMddHHmmss(list);
        return result;
    }

    @Override
    public String queryRcvMoneyBycustomer(String fromDate, String toDate, String customerName) {
        List<RcvMoney> list = accountDetailMapper.queryRcvMoneyBycustomer(fromDate, toDate,customerName);
        result = JSONTools.toStringyyyyMMddHHmmss(list);
        return result;
    }
}
