//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nccloud.web.erm.action.billmanage;

import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import nc.bs.logging.Log;
import nc.nccloud.util.erm.expenseaccount.Func;
import nc.vo.ep.bx.JKBXHeaderVO;
import nc.vo.ml.NCLangRes4VoTransl;
import nccloud.dto.baseapp.querytree.dataformat.Condition;
import nccloud.dto.baseapp.querytree.dataformat.PageInfo;
import nccloud.dto.baseapp.querytree.dataformat.QueryCondition;
import nccloud.dto.baseapp.querytree.dataformat.QueryTreeFormatVO;
import nccloud.dto.baseapp.querytree.dataformat.Value;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.model.row.Row;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.framework.web.ui.pattern.grid.GridOperator;
import nccloud.pubitf.erm.billmanage.IBillManageService;
import nccloud.web.erm.action.lic.AbstractLicAction;
import nccloud.web.erm.util.expenseaccount.initbills.InitBillsUtil;
import org.apache.commons.lang3.StringUtils;

public class BillQueryAction extends AbstractLicAction {
    private Log log = Log.getInstance(this.getClass());

    public BillQueryAction() {
    }

    public Object doRealAction(IRequest request) {
        JKBXHeaderVO[] jkbxvos = new JKBXHeaderVO[0];
        Grid grid = new Grid();
        PageInfo pageInfo = null;

        try {
            if (request == null) {
                ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("20110011makebill", "020110011makebill-0000"));
            }

            IJson json = JsonFactory.create();
            String read = request.read();
            if (StringUtils.isEmpty(read)) {
                ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("20110011makebill", "020110011makebill-0000"));
            }

            QueryTreeFormatVO info = (QueryTreeFormatVO)json.fromJson(read, QueryTreeFormatVO.class);
            if (null == info) {
                ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("20110011makebill", "020110011makebill-0000"));
            }

            if (info.getUserdefObj() != null) {
                QueryCondition queryCon = new QueryCondition();
                List<Condition> conditions = new ArrayList();

                for(Map.Entry<String, Object> entry : info.getUserdefObj().entrySet()) {
                    Condition con = new Condition();
                    con.setField((String)entry.getKey());
                    Value value = new Value();
                    value.setFirstvalue((String)entry.getValue());
                    con.setValue(value);
                    conditions.add(con);
                }

                queryCon.setConditions(conditions);
                info.setQuerycondition(queryCon);
            }

            IBillManageService billManageService = (IBillManageService)ServiceLocator.find(IBillManageService.class);
            String[] pks = null;
            if (info.getQuerycondition() != null) {
                jkbxvos = (JKBXHeaderVO[])billManageService.queryBillInfoByPage(info).toArray(jkbxvos);
            } else {
                ExceptionUtils.wrapBusinessException("请传入查询条件");
            }

            if (jkbxvos == null || jkbxvos.length == 0) {
                return null;
            }

            pks = new String[jkbxvos.length];

            for(int i = 0; i < jkbxvos.length; ++i) {
                if (jkbxvos[i] != null) {
                    pks[i] = jkbxvos[i].getPk_jkbx();
                }
            }

            GridOperator go = new GridOperator("201104DJCX_LIST");
            grid = go.toGrid(jkbxvos);
            InitBillsUtil.proGridDigit(grid);
            this.proGridDjztAndSpr(grid, pks);
            pageInfo = info.getPageInfo();
        } catch (Exception e) {
            this.log.error(e);
            ExceptionUtils.wrapException(e);
        }

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("pageInfo", pageInfo);
        jsonObj.put("data", grid);
        return jsonObj;
    }

    private void proGridDjztAndSpr(Grid grid, String[] pks) throws Exception {
        IBillManageService service = (IBillManageService)ServiceLocator.find(IBillManageService.class);
        Map<String, String> busiid_username = service.qryCurApproveman(Arrays.asList(pks));
        Row[] rows = grid.getModel().getRows();

        for(int i = 0; i < rows.length; ++i) {
            String pk_billtype = Func.toString(rows[i].getCell("pk_billtype").getValue());
            String djlxbm = Func.toString(rows[i].getCell("djlxbm").getValue());
            if (!"264a".equalsIgnoreCase(djlxbm) && !"261X".equalsIgnoreCase(pk_billtype) && !"262X".equalsIgnoreCase(pk_billtype)) {
                if ("3".equals(Func.toString(rows[i].getCell("djzt").getValue()))) {
                    rows[i].getCell("djzt").setDisplay(NCLangRes4VoTransl.getNCLangRes().getStrByID("20110012billmanage", "020110012billmanage-0002"));
                }
            } else if ("3".equals(Func.toString(rows[i].getCell("djzt").getValue()))) {
                rows[i].getCell("djzt").setDisplay(NCLangRes4VoTransl.getNCLangRes().getStrByID("20110012billmanage", "020110012billmanage-0000"));
            }

            if (busiid_username != null && busiid_username.size() > 0) {
                String pk_jkbx = Func.toString(rows[i].getCell("pk_jkbx").getValue());
                if (!StringUtils.isEmpty((CharSequence)busiid_username.get(pk_jkbx))) {
                    rows[i].getCell("approver").setDisplay((String)busiid_username.get(pk_jkbx));
                }
            }
        }

    }
}
