package com.goafanti.expenseAccount.service.impl;

import com.goafanti.common.dao.ExpenseAccountMapper;
import com.goafanti.common.dao.ExpenseAccountPrivateMapper;
import com.goafanti.common.model.ExpenseAccount;
import com.goafanti.common.model.ExpenseAccountPrivate;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.expenseAccount.bo.InputExpenseAccountPrivate;
import com.goafanti.expenseAccount.bo.OutExpenseAccountPrivate;
import com.goafanti.expenseAccount.service.ExpenseAccountPrivateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ExpenseAccountPrivateServiceImpl extends BaseMybatisDao<ExpenseAccountPrivateMapper> implements ExpenseAccountPrivateService {

    @Autowired
    private ExpenseAccountPrivateMapper expenseAccountPrivateMapper;
    @Autowired
    private ExpenseAccountMapper expenseAccountMapper;

    @Override
    public ExpenseAccountPrivate add(InputExpenseAccountPrivate in) {
        updateStatus(in);
        expenseAccountPrivateMapper.insertSelective(in);
        ExpenseAccountPrivate res=in;
        if (in.getEaid()!=null){
            ExpenseAccount ea =new ExpenseAccount();
            ea.setId(in.getEaid());
            ea.setEaaid(res.getId());
            expenseAccountMapper.updateByPrimaryKeySelective(ea);
        }
        return res;
    }

    private void updateStatus(InputExpenseAccountPrivate in) {
        if (in.getStatus()==0){
            Integer defaultCount=chekAccountDefault(in.getAid());
            if (defaultCount>0){
                in.setStatus(0);
            }else {
                in.setStatus(1);
            }
        }else {
            expenseAccountPrivateMapper.updateStatusByAid(in);
        }
    }

    private Integer chekAccountDefault(String aid) {
        Integer defaultCount=0;
        InputExpenseAccountPrivate check=new InputExpenseAccountPrivate();
        check.setAid(aid);
        check.setStatus(1);
        defaultCount=expenseAccountPrivateMapper.checkParam(check);
        return defaultCount;
    }

    @Override
    public Object selectList(Integer status,Integer pageSize, Integer pageNo) {
        String aid=TokenManager.getAdminId();
        Map<String,Object> params=new HashMap<>();
        params.put("aid",aid);
        if (status!=null)params.put("status",status);
        Pagination<?> page = findPage("selectExpenseAccountPrivateList", "selectExpenseAccountPrivateCount", params, pageNo, pageSize);
        return page;
    }

    @Override
    public boolean addCheckAccounts(InputExpenseAccountPrivate in) {
        Integer checkCount =checkCount(in);
        if (checkCount>0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public int updateStatus(ExpenseAccountPrivate in) {
        InputExpenseAccountPrivate update=new InputExpenseAccountPrivate();
        update.setId(in.getId());
        update.setStatus(1);
        update.setAid(TokenManager.getAdminId());
        updateStatus(update);
        return expenseAccountPrivateMapper.updateByPrimaryKeySelective(update);
    }



    private Integer checkCount(InputExpenseAccountPrivate in) {
        InputExpenseAccountPrivate check=new InputExpenseAccountPrivate();
        check.setAid(in.getAid());
        check.setAccounts(in.getAccounts());
        return expenseAccountPrivateMapper.checkParam(check);
    }

    @Override
    public int update(InputExpenseAccountPrivate in) {
        updateStatus(in);
        return expenseAccountPrivateMapper.updateByPrimaryKeySelective(in);
    }


    @Override
    public boolean updateCheckAccounts(InputExpenseAccountPrivate in) {
        InputExpenseAccountPrivate checkParam=new InputExpenseAccountPrivate();
        checkParam.setAid(in.getAid());
        checkParam.setAccounts(in.getAccounts());
        checkParam.setName(in.getName());
        ExpenseAccountPrivate accountPrivate = expenseAccountPrivateMapper.selectByAccount(checkParam);
        if (accountPrivate==null||accountPrivate.getId().equals(in.getId())){
            return false;
        }else {
            return true;
        }

    }

    @Override
    public OutExpenseAccountPrivate details(Integer id) {
        return expenseAccountPrivateMapper.selectById(id);
    }
}
