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.common.CommonMapper;
import com.th.demo.mapping.finance.*;
import com.th.demo.model.finance.*;
import com.th.demo.model.system.SysUser;
import com.th.demo.service.finance.AccountService;
import com.th.demo.service.finance.InvoiceService;
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("InvoiceService")
public class InvoiceServiceImpl implements InvoiceService {
    @Autowired
    InvoiceMapper invoiceMapper;
    @Autowired
    InvoiceNewMapper invoiceNewMapper;

    @Autowired
    VerifyAccountMapper verifyAccountMapper;

    String result = "";

    int num = 0;

    @Override
    public String query( String accountId, String userId, String belongId) {

        List<Invoice> list = invoiceMapper.selectByFid(accountId,belongId);

        result = JSONTools.toStringyyyyMMddHHmmss(list);
        return result;
    }

    @Override
    public String save(String json, String userId, String belongId) {
        num = 0;
        Invoice  invoice = JSON.parseObject(json, new TypeReference<Invoice>() {
        });
        invoice.setAddUser(new SysUser(userId));
        invoice.setCancelFlag("0");
        invoice.setAddTime(new Date());
        invoice.setBelongId(belongId);
        System.out.println(invoice.getAccountId()+","+invoice.getPrice());
        num += invoiceMapper.insert(invoice);
        return Tools.moreThanZeroResultJSON(num);
    }

    @Override
    public String remove(String id, String userId, String belongId) {
        Invoice  invoice = invoiceMapper.selectByPrimaryKey(id);
        invoice.setCancelFlag("1");
        invoice.setCancelId(userId);
        invoice.setCancelTime(new Date());
        num += invoiceMapper.updateByPrimaryKey(invoice);
        return Tools.moreThanZeroResultJSON(num);
    }

    @Override
    public String queryInvoiceNew(String queryDateFrom, String queryDateTo, String fkComponeyName, String userId, String belongId) {
        List<VerifyAccount> list =  verifyAccountMapper.selectInvocie(queryDateFrom,queryDateTo,fkComponeyName);
        result = JSONTools.toStringyyyyMMddHHmmss(list);
        return result;
    }


}
