package com.dxhy.base.service.invoicecheck;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.base.entity.BaseTDxDkCount;
import com.dxhy.base.model.BaseBeforegxRespVO;
import com.dxhy.base.service.sqdktj.BaseStatisticalEnterpriseService;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.ApplyStatusEnum;
import com.dxhy.common.service.impl.MtBaseServiceImpl;

/**
 * 申请抵扣统计service实现类
 *
 * @author kangzq
 *
 */
@Service("baseBeforeGxService")
public class BaseBeforeGxServiceServiceImpl extends MtBaseServiceImpl implements BaseBeforeGxService {

    @Resource
    private BaseStatisticalEnterpriseService baseStatisticalEnterpriseService;

    @Resource
    private BaseInvoiceManualCheckService baseInvoiceManualCheckService;

    @Override
    public JSONObject selectByIds(String[] ids, Map<String, Object> pramsMap) throws ParseException {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        JSONObject json = new JSONObject();
        List<BaseBeforegxRespVO> voList = baseInvoiceManualCheckService.beforegx(ids);

        boolean flag = true;

        StringBuffer tsxx = new StringBuffer();
        tsxx.append("因以下税号税款所属期次月进行统计操作,提交勾选失败:");

        if (voList != null && voList.size() > 0) {
            for (int i = 0; i < voList.size(); i++) {
                BaseBeforegxRespVO vo;
                vo = voList.get(i);
                BaseTDxDkCount td = baseStatisticalEnterpriseService.selectByBeforeRespVO(vo);

                String cydit = vo.getCydyt();
                String sqsj = td.getCreateDate();
                String sqzt = td.getApplyStatus();

                if (!ApplyStatusEnum.WSQ.getApplyStatusDm().equals(sqzt)) {

                    if (sqsj != null) {
                        Date sqsj1 = new SimpleDateFormat("yyyy-MM-dd").parse(sqsj);
                        Date cydit1 = new SimpleDateFormat("yyyy-MM-dd").parse(cydit);

                        if (sqsj1.after(cydit1)) {
                            if (!tsxx.toString().contains(vo.getGfsh())) {
                                if (i < voList.size() - 1) {
                                    tsxx.append(vo.getGfsh()).append(",");
                                } else {
                                    tsxx.append(vo.getGfsh());
                                }

                            }
                            flag = false;
                        }
                    }
                }
            }
        }
        json.put("flag", flag);
        json.put("tsxx", tsxx);
        return json;
    }

}
