package com.dxhy.base.service.receipt;

import java.util.*;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.base.dao.BaseReceiptOutDao;
import com.dxhy.base.dao.BaseTDxOutReasonDao;
import com.dxhy.base.entity.BaseTDxOutReason;
import com.dxhy.base.entity.BaseTDxRecordInvoiceOut;
import com.dxhy.base.model.BaseOutReasonResVO;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.utils.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @author ld
 */
@SuppressWarnings("AlibabaClassNamingShouldBeCamel")
@Service
public class BaseTDxOutReasonServiceImpl extends MpBaseServiceImpl<BaseTDxOutReasonDao, BaseTDxOutReason>
    implements BaseTDxOutReasonService {

    @Resource
    private BaseReceiptOutDao baseReceiptOutDao;

    @Override
    public String listOutReason(Map<String, String> pramsMap) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        String[] titlelist = {"序号", "转出原因", "创建时间", "创建人"};
        String[] titleprop = {"xh", "outReason", "createDate", "createUser"};

        // 设置title
        JSONObject json = super.setTitleList(titlelist, titleprop);

        // 分页参数
        PageHelper.startPage(Integer.parseInt(pramsMap.get("curr")), Integer.parseInt(pramsMap.get("size")));
        QueryWrapper<BaseTDxOutReason> query = new QueryWrapper<>();
        List<String> cmpy = new ArrayList<>();
        cmpy.add("DX");
        cmpy.add(pramsMap.get("company"));
        query.in("company", cmpy);
        query.orderByAsc("id");
        List<BaseTDxOutReason> list = super.baseMapper.selectList(query);
        PageInfo<BaseTDxOutReason> pageInfo = new PageInfo<>(list);
        // 查询结果封装为view
        List<BaseOutReasonResVO> respVos = new ArrayList<>();
        int start = new Long(pageInfo.getStartRow()).intValue();

        for (int i = 0; i < pageInfo.getList().size(); i++) {
            BaseTDxOutReason view = pageInfo.getList().get(i);
            BaseOutReasonResVO vo = new BaseOutReasonResVO();
            vo.setId(view.getId().toString());
            vo.setXh(start + i);
            vo.setReasonCode(view.getReasonCode());
            vo.setOutReason(view.getOutReason());
            vo.setCreateDate(DateUtils.dateToStr(view.getCreateDate()));
            vo.setCreateUser(view.getCreateUser());
            respVos.add(vo);
        }

        // 设置记录总数
        json.put("total", pageInfo.getTotal());
        json.put("datalist", respVos);
        return super.responseResult(json);
    }

    @Override
    public int addOutReason(Map<String, String> pramsMap) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        String reason = pramsMap.get("outReason");
        QueryWrapper<BaseTDxOutReason> query = new QueryWrapper<>();
        query.eq("out_reason", reason.trim());

        BaseTDxOutReason baseOutReason = super.baseMapper.selectOne(query);

        if (baseOutReason != null) {

            return 1;
        }
        BaseTDxOutReason newReason = new BaseTDxOutReason();
        String company = pramsMap.get("company");
        newReason.setCompany(company);
        newReason.setCreateDate(new Date());
        newReason.setCreateUser(pramsMap.get("userName"));
        newReason.setOutReason(reason.trim());
        String code = company + "_" + UUID.randomUUID();
        newReason.setReasonCode(code.replace("-", ""));

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_WRITE);
        baseMapper.insert(newReason);
        return 0;
    }

    @Override
    public int deleteOutReason(Map<String, String> pramsMap) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        QueryWrapper<BaseTDxRecordInvoiceOut> outQuery = new QueryWrapper<>();
        outQuery.eq("out_reason", pramsMap.get("reasonCode"));
        List<BaseTDxRecordInvoiceOut> recordInvoiceOuts = baseReceiptOutDao.selectList(outQuery);

        if (recordInvoiceOuts != null && recordInvoiceOuts.size() > 0) {
            return 1;
        }

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_WRITE);
        super.baseMapper.deleteById(Long.valueOf(pramsMap.get("id")));

        return 0;
    }

    @Override
    public int updateOutReason(Map<String, String> pramsMap) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        String reason = pramsMap.get("outReason");

        QueryWrapper<BaseTDxRecordInvoiceOut> outQuery = new QueryWrapper<>();
        outQuery.eq("out_reason", pramsMap.get("reasonCode"));
        List<BaseTDxRecordInvoiceOut> recordInvoiceOuts = baseReceiptOutDao.selectList(outQuery);

        if (recordInvoiceOuts != null && recordInvoiceOuts.size() > 0) {
            return 2;
        }

        QueryWrapper<BaseTDxOutReason> query = new QueryWrapper<>();
        query.eq("out_reason", reason.trim());

        BaseTDxOutReason baseOutReason = super.baseMapper.selectOne(query);
        if (baseOutReason != null) {

            return 1;
        }

        BaseTDxOutReason newReason = new BaseTDxOutReason();
        newReason.setUpdateDate(new Date());
        newReason.setOutReason(reason.trim());
        newReason.setId(Long.valueOf(pramsMap.get("id")));
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_WRITE);
        baseMapper.updateById(newReason);

        return 0;
    }

}
