package com.engine.salary.wrapper;

import com.cloudstore.eccom.result.WeaResultMsg;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.component.SalaryWeaTable;
import com.engine.salary.entity.datacollection.bo.AttendQuoteFieldBO;
import com.engine.salary.entity.datacollection.dto.AttendQuoteFieldFormDTO;
import com.engine.salary.entity.datacollection.dto.AttendQuoteFieldListDTO;
import com.engine.salary.entity.datacollection.param.AttendQuoteFieldQueryParam;
import com.engine.salary.entity.datacollection.param.AttendQuoteFieldSaveParam;
import com.engine.salary.entity.datacollection.po.AttendQuoteFieldPO;
import com.engine.salary.enums.datacollection.AttendQuoteFieldSourceTypeEnum;
import com.engine.salary.enums.datacollection.AttendQuoteFieldTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.AttendQuoteFieldService;
import com.engine.salary.service.impl.AttendQuoteFieldServiceImpl;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.page.PageInfo;
import org.springframework.beans.BeanUtils;
import weaver.hrm.User;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 考勤引用字段
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class AttendQuoteFieldWrapper extends Service {

    private AttendQuoteFieldService getAttendQuoteFieldService(User user) {
        return  ServiceUtil.getService(AttendQuoteFieldServiceImpl.class, user);
    }

    /**
     * 考勤字段管理列表(分页)
     *
     * @param queryParam
     * @return
     */
    public Map<String, Object> list(AttendQuoteFieldQueryParam queryParam) {
        // 同步字段
        syncAttendFields();


        PageInfo<AttendQuoteFieldListDTO> page = getAttendQuoteFieldService(user).listPage(queryParam);

        List<AttendQuoteFieldListDTO> list = page.getList();
        list.forEach(m->{
            Integer sourceType = Integer.valueOf(m.getSourceType());
            m.setSourceType(AttendQuoteFieldSourceTypeEnum.getDefaultLabelByValue(sourceType));
            m.setSourceTypeValue(AttendQuoteFieldSourceTypeEnum.getNameByValue(sourceType));
            m.setFieldType(AttendQuoteFieldTypeEnum.getDefaultLabelByValue(Integer.valueOf(m.getFieldType())));
        });
        page.setList(list);

        SalaryWeaTable<AttendQuoteFieldListDTO> table = new SalaryWeaTable<>(user, AttendQuoteFieldListDTO.class);
        table.setColumns(AttendQuoteFieldBO.buildRecordTableColumns());


        WeaResultMsg result = new WeaResultMsg(false);
        result.putAll(table.makeDataResult());
        result.success();

        Map<String,Object> datas = new HashMap<>();
        datas.put("pageInfo", page);
        datas.put("dataKey",result.getResultMap());

        return datas;
    }

    /**
     * 同步考勤模块字段
     *
     * @return
     */
    public String syncAttendFields() {
        return getAttendQuoteFieldService(user).syncAttendFields();
    }

    /**
     * 获取考勤字段表单
     *
     * @param id
     * @return
     */
    public AttendQuoteFieldFormDTO getFrom(Long id) {
        AttendQuoteFieldFormDTO formDTO = new AttendQuoteFieldFormDTO();
        if (id != null) {
            AttendQuoteFieldPO po = getAttendQuoteFieldService(user).getById(id);
            if (po == null) {
                throw new SalaryRunTimeException(String.format(SalaryI18nUtil.getI18nLabel( 100370, "字段不存在")+"[id:%s]", id));
            }
            BeanUtils.copyProperties(po, formDTO);
        } else {
            formDTO.setEnableStatus(0);
        }

        return formDTO;
    }

    /**
     * 新建考勤字段
     *
     * @param saveParam
     * @return
     */
    public String save(AttendQuoteFieldSaveParam saveParam) {
        return getAttendQuoteFieldService(user).save(saveParam);
    }

    /**
     * 启用/停用自定义字段
     *
     * @return
     */
    public String updateEnableStatus(AttendQuoteFieldSaveParam param) {
        return getAttendQuoteFieldService(user).updateEnableStatus(param);
    }

    /**
     * 修改考勤字段
     *
     * @param saveParam
     * @return
     */
    public String update(AttendQuoteFieldSaveParam saveParam) {
        return getAttendQuoteFieldService(user).update(saveParam);
    }

    /**
     * 删除考勤字段
     *
     * @param ids
     * @return
     */
    public String delete(Collection<Long> ids) {
        return getAttendQuoteFieldService(user).delete(ids);
    }
}
