package com.kingdee.eas.custom.hireapproval.handler;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.hireapproval.IOAHireSalaryData;
import com.kingdee.eas.custom.hireapproval.OAHireSalaryDataFactory;
import com.kingdee.eas.custom.hireapproval.OAHireSalaryDataInfo;
import com.kingdee.eas.custom.hireapproval.bizEnum.ExecuteStateEnum;
import com.kingdee.eas.custom.hireapproval.task.HireApprovalFacadeFactory;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.json.GridDataEntity;
import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
import com.kingdee.util.StringUtils;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description OA录用定薪数据ListHandler
 * @Date 2025/7/31 16:33
 * @Created by Heyuan
 */
public class OAHireSalaryDataListHandler extends ListHandler {
    /**
     * 执行入职定薪
     *
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     */
    public void executeHireSalaryBillAction(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws SHRWebException {
        try {
            Context ctx = SHRContext.getInstance().getContext();
            String billId = getBillId(request);
            if (StringUtils.isEmpty(billId)) {
                throw new ShrWebBizException("请先选中数据!");
            }
            IOAHireSalaryData ioaHireSalaryData = OAHireSalaryDataFactory.getLocalInstance(ctx);
            OAHireSalaryDataInfo oaHireSalaryDataInfo =
                    ioaHireSalaryData.getOAHireSalaryDataInfo(new ObjectUuidPK(billId));
            ExecuteStateEnum executeStatus = oaHireSalaryDataInfo.getExecuteStatus();
            if (ExecuteStateEnum.SUCCESS.equals(executeStatus)) {
                throw new ShrWebBizException("成功状态的数据不允许重复执行");
            } else if (ExecuteStateEnum.EXECUTEING.equals(executeStatus)) {
                throw new ShrWebBizException("执行中状态的数据不允许重复执行");
            }
            HireApprovalFacadeFactory.getLocalInstance(ctx).createHireSalaryBill(billId);
        } catch (BOSException e) {
            e.printStackTrace();
            throw new ShrWebBizException(e);
        } catch (EASBizException e) {
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }
    }

    @Override
    protected void afterGetListData(
            HttpServletRequest request,
            HttpServletResponse response,
            GridDataEntity gridDataEntity
    ) throws SHRWebException {
        super.afterGetListData(request, response, gridDataEntity);
        List<Map<String, Object>> rows = gridDataEntity.getRows();
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        Map<String, Map<String, Object>> rowData = new HashMap<String, Map<String, Object>>();
        for (int i = 0; i < rows.size(); i++) {
            Map<String, Object> map = rows.get(i);
            String id = (String) map.get("id");
            String cmpItemNumber = (String) map.get("cmpItem.number");
            Object money = map.get("entrys.money");
            if (rowData.containsKey(id)) {
                rowData.get(id).put(cmpItemNumber, money);
            } else {
                map.put(cmpItemNumber, money);
                String idNumber = (String) map.get("idNumber");
                String passport = (String) map.get("passport");
                if (!StringUtils.isEmpty(idNumber)) {
                    map.put("identification", idNumber);
                } else {
                    map.put("identification", passport);
                }
                rowData.put(id, map);
            }
        }
        rowData.entrySet().stream().forEach(entry -> {
            data.add(entry.getValue());
        });
        int rowPage = Integer.parseInt(request.getParameter("rows"));
        gridDataEntity.setTotal(data.size() % rowPage == 0 ? data.size() / rowPage : data.size() / rowPage + 1);
        gridDataEntity.setRecords(data.size());
        gridDataEntity.setRows(data);
    }

    public void afterGetExportData(
            HttpServletRequest request,
            HttpServletResponse response,
            List listData
    ) throws SHRWebException {
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        Map<String, Map<String, Object>> rowData = new HashMap<String, Map<String, Object>>();
        for (int i = 0; i < listData.size(); i++) {
            Map<String, Object> map = (Map<String, Object>) listData.get(i);
            String id = (String) map.get("id");
            String cmpItemNumber = (String) map.get("cmpItem.number");
            Object money = map.get("entrys.money");
            if (rowData.containsKey(id)) {
                rowData.get(id).put(cmpItemNumber, money);
            } else {
                map.put(cmpItemNumber, money);
                String idNumber = (String) map.get("idNumber");
                String passport = (String) map.get("passport");
                if (!StringUtils.isEmpty(idNumber)) {
                    map.put("identification", idNumber);
                } else {
                    map.put("identification", passport);
                }
                rowData.put(id, map);
            }
        }
    }
}
