package ink.wgink.module.form.service.design.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.form.dao.design.IFormFieldDao;
import ink.wgink.module.form.pojo.dtos.design.FormFieldDTO;
import ink.wgink.module.form.pojo.pos.design.FormFieldPO;
import ink.wgink.module.form.pojo.vos.design.FormFieldVO;
import ink.wgink.module.form.service.design.IFormFieldService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FormFieldServiceImpl extends DefaultBaseService implements IFormFieldService {

    @Autowired
    private IFormFieldDao formFieldDao;

    @Override
    public void save(FormFieldVO formFieldVO) {
        saveReturnId(formFieldVO);
    }

    @Override
    public String saveReturnId(FormFieldVO formFieldVO) {
        String fieldId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(formFieldVO);
        params.put("fieldId", fieldId);
        setSaveInfo(params);
        formFieldDao.save(params);
        return fieldId;
    }

    @Override
    public void delete(List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("formFieldIds", ids);
        formFieldDao.delete(params);
    }

    @Override
    public void deleteByFormIds(List<String> formIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("formIds", formIds);
        formFieldDao.delete(params);
    }

    @Override
    public void update(String fieldId, FormFieldVO formFieldVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(formFieldVO);
        params.put("fieldId", fieldId);
        setUpdateInfo(params);
        formFieldDao.update(params);
    }

    @Override
    public FormFieldDTO get(Map<String, Object> params) {
        params = params == null ? getHashMap(0) : params;
        return formFieldDao.get(params);
    }

    @Override
    public FormFieldDTO get(String fieldId) {
        Map<String, Object> params = getHashMap(2);
        params.put("fieldId", fieldId);
        return get(params);
    }

    @Override
    public FormFieldPO getPO(Map<String, Object> params) {
        params = params == null ? getHashMap(0) : params;
        return formFieldDao.getPO(params);
    }

    @Override
    public FormFieldPO getPO(String fieldId) {
        Map<String, Object> params = getHashMap(2);
        params.put("fieldId", fieldId);
        return getPO(params);
    }

    @Override
    public List<FormFieldDTO> list(Map<String, Object> params) {
        params = params == null ? getHashMap(2) : params;
        return formFieldDao.list(params);
    }

    @Override
    public List<FormFieldDTO> listByFormId(String formId) {
        Map<String, Object> params = getHashMap(2);
        params.put("formId", formId);
        return list(params);
    }

    @Override
    public List<FormFieldPO> listPO(Map<String, Object> params) {
        params = params == null ? getHashMap(2) : params;
        return formFieldDao.listPO(params);
    }

    @Override
    public List<FormFieldPO> listPOByFormId(String formId) {
        Map<String, Object> params = getHashMap(2);
        params.put("formId", formId);
        return listPO(params);
    }

    @Override
    public SuccessResultList<List<FormFieldDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<FormFieldDTO> dataDTOs = list(page.getParams());
        PageInfo<FormFieldDTO> pageInfo = new PageInfo<>(dataDTOs);
        return new SuccessResultList<>(dataDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public List<String> listFieldName(Map<String, Object> params) {
        params = params == null ? getHashMap(0) : params;
        return formFieldDao.listFieldName(params);
    }

    @Override
    public List<String> listFieldNameByFormIdAndListShow(String formId, Boolean isListShow) {
        Map<String, Object> params = getHashMap(2);
        params.put("formId", formId);
        params.put("listShow", isListShow ? ISystemConstant.IS_TRUE : ISystemConstant.IS_FALSE);
        return listFieldName(params);
    }

    @Override
    public List<FormFieldDTO> listByFormCodeAndVersion(String formCode, Integer formVersion) {
        Map<String, Object> params = getHashMap(4);
        params.put("formCode", formCode);
        params.put("formVersion", formVersion);
        return list(params);
    }
}
